+ All Categories
Home > Internet > Views plugins-in-d7-and-d8

Views plugins-in-d7-and-d8

Date post: 12-Jul-2015
Category:
Upload: frank-holldorff
View: 270 times
Download: 0 times
Share this document with a friend
30
# Views Plugins in Drupal 7 and Drupal 8 Michael Lenahan, Developer at erdfisch http://drupalcamp.berlin/program/sessions/views-plugins- d7-and-d8
Transcript
Page 1: Views plugins-in-d7-and-d8

# Views Plugins in Drupal 7 and Drupal 8

● Michael Lenahan, Developer at erdfisch

● http://drupalcamp.berlin/program/sessions/views­plugins­d7­and­d8

Page 2: Views plugins-in-d7-and-d8

# Key Points

● In D7, views is extensible through plugins

● In D8, all of Drupal is extensible through plugins

● In D7, Views is object­oriented

● In D8, All of Drupal is object­oriented

Page 3: Views plugins-in-d7-and-d8

# tutorial and documentation# Source: http://www.codem0nk3y.com/2012/04/what­bugs­me­about­modx­and­why/cms­learning­curve/

Page 4: Views plugins-in-d7-and-d8

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/site­information

# add some content

Page 5: Views plugins-in-d7-and-d8

Drupal 7 ­­­ setup

Page 6: Views plugins-in-d7-and-d8

# Drupal 7# Mummy, where do views plugins come from?

Page 7: Views plugins-in-d7-and-d8

# 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().

Page 8: Views plugins-in-d7-and-d8

# drupal 7cd sites/all/modules/contrib/views/plugins && ls

Page 9: Views plugins-in-d7-and-d8

class views_plugin_access_none extends views_plugin_accessclass views_plugin_access_perm extends views_plugin_accessclass views_plugin_access_role extends views_plugin_access

Page 10: Views plugins-in-d7-and-d8

# using sublime text (ctrl p)# alternatively, use grep on the command line:

grep ­rin 'extends views_plugin' .

Page 11: Views plugins-in-d7-and-d8

# 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' => 'access­none',

       ),

Page 12: Views plugins-in-d7-and-d8

# Let's create a D7 custom views access plugin

Page 13: Views plugins-in-d7-and-d8

# 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.

Page 14: Views plugins-in-d7-and-d8

# Enable our empty D7 module by browsing to:admin/modules

Page 15: Views plugins-in-d7-and-d8

# 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',

  );

}

Page 16: Views plugins-in-d7-and-d8

# 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;

}

Page 17: Views plugins-in-d7-and-d8

# 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

Page 18: Views plugins-in-d7-and-d8

# Clear the cache (drush cc views or admin/config/development/performance)

# Reload: admin/structure/views/view/frontpage/edit

Page 19: Views plugins-in-d7-and-d8
Page 20: Views plugins-in-d7-and-d8

# 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');

  }

Page 21: Views plugins-in-d7-and-d8

# 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;

}

Page 22: Views plugins-in-d7-and-d8

# This Demo is brought to you by Funny Cat

Page 23: Views plugins-in-d7-and-d8

# 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

Page 24: Views plugins-in-d7-and-d8

# Enable our empty D8 module by browsing to:admin/modules

Page 25: Views plugins-in-d7-and-d8

# 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 drupal­8 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

Page 26: Views plugins-in-d7-and-d8

# my_views_plugins/src/Plugin/views/access/Time.php

namespace Drupal\my_views_plugins\Plugin\views\access;

use Drupal\Core\Session\AccountInterface;

use Symfony\Component\Routing\Route;

use Drupal\views\Plugin\views\access\AccessPluginBase;

class Time extends AccessPluginBase {

  public function summaryTitle() {}

  public function access(AccountInterface $account) {}

  public function alterRouteDefinition(Route $route) {}

}

Page 27: Views plugins-in-d7-and-d8

# 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');

  }

}

Page 28: Views plugins-in-d7-and-d8

# my_views_plugins/my_views_plugins.module

function _my_views_plugin_access() {

  $date = new \DateTime('now');

  $hour = $date­>format('H');

  if ($hour >= 22) {

    return \Drupal\Core\Access\AccessResult::allowed();

  }

  return \Drupal\Core\Access\AccessResult::forbidden();

}

Page 29: Views plugins-in-d7-and-d8

# D8 Demo

Page 30: Views plugins-in-d7-and-d8

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


Recommended