Views plugins in Drupal7 and Drupal8. This are the slides from Michael Lenahans session http://drupalcamp.berlin/program/sessions/viewspluginsd7andd8
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
Views plugins-in-d7-and-d8
1. # Views Plugins in Drupal 7 and Drupal 8
● Michael Lenahan, Developer at erdfisch
● http://drupalcamp.berlin/program/sessions/viewspluginsd7andd8
2. # Key Points
● In D7, views is extensible through plugins
● In D8, all of Drupal is extensible through plugins
● In D7, Views is objectoriented
● In D8, All of Drupal is objectoriented
3. # tutorial and documentation
# Source: http://www.codem0nk3y.com/2012/04/whatbugsmeaboutmodxandwhy/
cmslearningcurve/
4. Drupal 7 setup
# enable views and views_ui
drush en y
views_ui
# enable the frontpage view at:
# admin/structure/views
# set 'Default front page' to 'frontpage' at:
# admin/config/system/siteinformation
# add some content
6. # Drupal 7
# Mummy, where do views plugins come from?
7. # Look in s/a/m/contrib/views/includes/plugins.inc
# All the 'native' views plugins are declared there.
/**
* Implements hook_views_plugins().
*/
function views_views_plugins() {
$plugins = array(
// display, style, row, argument default, argument
validator and access.
● So we will also need to implement hook_views_plugins().
8. # drupal 7
cd sites/all/modules/contrib/views/plugins && ls
9. class views_plugin_access_none extends views_plugin_access
class views_plugin_access_perm extends views_plugin_access
class views_plugin_access_role extends views_plugin_access
10. # using sublime text (ctrl p)
# alternatively, use grep on the command line:
grep rin
'extends views_plugin' .
11. # Look in s/a/m/contrib/views/includes/plugins.inc
# Views declares its own plugins by implementing
hook_views_plugins().
function views_views_plugins() {
$plugins = array(
// display, style, row, argument default, argument
validator and access.
'access' => array(
'none' => array(
'title' => t('None'),
'help' => t('Will be available to all users.'),
'handler' => 'views_plugin_access_none',
'help topic' => 'accessnone',
),
13. # Let's create a D7 custom views access plugin
mkdir p
sites/all/modules/custom/my_views_plugins
cd sites/all/modules/custom/my_views_plugins
# Create a file named my_views_plugins.info:
name = My Views Plugins
description = Some views plugins examples.
core = 7.x
# Create an empty file named my_views_plugins.module.
14. # Enable our empty D7 module by browsing to:
admin/modules
15. # In D7, we need to tell Drupal we are using views.
# Copy the following text into my_views_plugins.module:
<?php
/**
* Implements hook_views_api().
*/
function my_views_plugin_views_api() {
return array(
'api' => '3',
);
}
16. # In D7, we need to implement hook_views_plugins().
# Create the following file:
s/a/m/custom/my_views_plugins/my_views_plugin.views.inc
function my_views_plugin_views_plugins() {
$plugins = array();
// Tip: copy from: sites/all/modules/contrib/views/includes/plugins.inc
$plugins['access'] = array(
'time' => array(
'title' => t('Time'),
'help' => t('Access will be granted according to time of day.'),
'handler' => 'views_plugin_access_time',
),
);
return $plugins;
}
17. # We still need to tell D7 about the .inc file.
# Add the files[] line to my_views_plugin.info:
name = My Views Plugins
description = Some views plugins examples.
core = 7.x
files[] = plugins/views_plugin_access_time.inc
18. # Clear the cache (drush cc views or
admin/config/development/performance)
# Reload: admin/structure/views/view/frontpage/edit
19.
20. # Now write the actual plugin class:
# my_views_plugins/plugins/views_plugin_access_time.inc
class views_plugin_access_time extends
views_plugin_access {
// Override the base class methods.
function access($account) {
return _my_views_plugin_access();
}
function get_access_callback() {
return array('_my_views_plugin_access');
}
21. # my_views_plugin.module
function _my_views_plugin_access() {
$date = new DateTime('now');
$hour = $date>
format('H');
if ($hour > 22) {
// The children are in bed.
return TRUE;
}
return FALSE;
}
23. # Views Plugins in Drupal 8
mkdir p
modules/custom/my_views_plugins
cd modules/custom/my_views_plugins
# Create a file named my_views_plugins.info.yml
name: My Views Plugins
type: module
description: Some views plugins examples.
core: 8.x
# Create an empty my_views_plugins.module file
<?php
24. # Enable our empty D8 module by browsing to:
admin/modules
25. # In Drupal 8, views access plugins are to be found in a
predictable location.
# This means that they can be easily discovered!
core/modules/user/src/Plugin/views/access/Permission.php
core/modules/user/src/Plugin/views/access/Role.php
core/modules/views/src/Plugin/views/access/None.php
● So, let's create the same directory structure …
(cd to your drupal8
root)
mkdir p
modules/custom/my_views_plugins/src/Plugin/views/access
cd modules/custom/my_views_plugins/src/Plugin/views/access
● create a Time.php file here, copy None.php to start
26. # my_views_plugins/src/Plugin/views/access/Time.php
namespace Drupalmy_views_pluginsPluginviewsaccess;
use DrupalCoreSessionAccountInterface;
use SymfonyComponentRoutingRoute;
use DrupalviewsPluginviewsaccessAccessPluginBase;
class Time extends AccessPluginBase {
public function summaryTitle() {}
public function access(AccountInterface $account) {}
public function alterRouteDefinition(Route $route) {}
}
27. # my_views_plugins/src/Plugin/views/access/Time.php
# Implement all the methods of AccessPluginBase
class Time extends AccessPluginBase {
public function summaryTitle() {
return $this>
t('Restricted to a time of day.');
}
public function access(AccountInterface $account) {
return _my_views_plugin_access();
}
public function alterRouteDefinition(Route $route) {
$route>
setRequirement('_custom_access',
'_my_views_plugin_access');
}
}
28. # my_views_plugins/my_views_plugins.module
function _my_views_plugin_access() {
$date = new DateTime('now');
$hour = $date>
format('H');
if ($hour >= 22) {
return DrupalCoreAccessAccessResult::allowed();
}
return DrupalCoreAccessAccessResult::forbidden();
}
30. Thank you!
Further learning:
● Larry Garfield
DrupalCon Amsterdam 2014: Drupal 8: The Crash Course
● Joe Shindelar
DrupalCon Amsterdam 2014: An Overview of the Drupal 8
Plugin System