pastebin

Paste Search Dynamic
Recent pastes
entity bundle
  1. <?php
  2. // $Id$
  3.  
  4. /**
  5.  * @file
  6.  * Plugin to provide access control based upon entity bundle.
  7.  */
  8.  
  9. /**
  10.  * Plugins are described by creating a $plugin array which will be used
  11.  * by the system that includes this file.
  12.  */
  13. $plugin = array(
  14.   'title' => t("Entity: bundle"),
  15.   'description' => t('Control access by entity bundle.'),
  16.   'callback' => 'ctools_entity_bundle_ctools_access_check',
  17.   'default' => array('type' => array()),
  18.   'settings form' => 'ctools_entity_bundle_ctools_access_settings',
  19.   'settings form submit' => 'ctools_entity_bundle_ctools_access_settings_submit',
  20.   'summary' => 'ctools_entity_bundle_ctools_access_summary',
  21.   'restrictions' => 'ctools_entity_bundle_ctools_access_restrictions',
  22.   'get child' => 'ctools_entity_bundle_ctools_access_get_child',
  23.   'get children' => 'ctools_entity_bundle_ctools_access_get_children',
  24. );
  25.  
  26. function ctools_entity_bundle_ctools_access_get_child($plugin, $parent, $child) {
  27.   $plugins = ctools_entity_bundle_ctools_access_get_children($plugin, $parent);
  28.   return $plugins[$parent . ':' . $child];
  29. }
  30.  
  31. function ctools_entity_bundle_ctools_access_get_children($plugin, $parent) {
  32.   $entities = entity_get_info();
  33.   $plugins = array();
  34.   foreach ($entities as $entity_type => $entity) {
  35.     $plugin['title'] = t('@entity: Bundle', array('@entity' => $entity['label']));
  36.     $plugin['keyword'] = $entity_type;
  37.     $plugin['description'] = t('Control access by @entity entity bundle.', array('@entity' => $entity_type));
  38.     $plugin['name'] = $parent . ':' . $entity_type;
  39.     $plugin['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type);
  40.     $plugins[$parent . ':' . $entity_type] = $plugin;
  41.   }
  42.  
  43.   return $plugins;
  44. }
  45.  
  46. /**
  47.  * Settings form for the 'by entity_bundle' access plugin
  48.  */
  49. function ctools_entity_bundle_ctools_access_settings($form, &$form_state, $conf) {
  50.   $plugin = $form_state['plugin'];
  51.   $entity_type = explode(':', $plugin['name']);
  52.   $entity_type = $entity_type[1];
  53.   $entity = entity_get_info($entity_type);
  54.   foreach ($entity['bundles'] as $type => $info) {
  55.     $options[$type] = check_plain($info['label']);
  56.   }
  57.  
  58.   $form['settings']['type'] = array(
  59.     '#title' => t('Entity Bundle'),
  60.     '#type' => 'checkboxes',
  61.     '#options' => $options,
  62.     '#description' => t('Only the checked entity bundles will be valid.'),
  63.     '#default_value' => $conf['type'],
  64.   );
  65.   return $form;
  66. }
  67.  
  68. /**
  69.  * Compress the entity bundles allowed to the minimum.
  70.  */
  71. function ctools_entity_bundle_ctools_access_settings_submit($form, &$form_state) {
  72.   $form_state['values']['settings']['type'] = array_filter($form_state['values']['settings']['type']);
  73. }
  74.  
  75. /**
  76.  * Check for access.
  77.  */
  78. function ctools_entity_bundle_ctools_access_check($conf, $context, $plugin) {
  79.   list($plugin_name, $entity_type) = explode(':', $plugin['name']);
  80.   if (!$entity_type) {
  81.     return false;
  82.   };
  83.  
  84.   $entity = entity_get_info($entity_type);
  85.   // As far as I know there should always be a context at this point, but this
  86.   // is safe.
  87.   if (empty($context) || empty($context->data) || empty($context->data->{$entity['entity keys']['bundle']})) {
  88.     return false;
  89.   }
  90.  
  91.   if (array_filter($conf['type']) && empty($conf['type'][$context->data->{$entity['entity keys']['bundle']}])) {
  92.     return false;
  93.   }
  94.  
  95.   return true;
  96. }
  97.  
  98. /**
  99.  * Inform the UI that we've eliminated a bunch of possibilities for this
  100.  * context.
  101.  */
  102. function ctools_entity_bundle_ctools_access_restrictions($conf, &$context) {
  103.   if (isset($context->restrictions['type'])) {
  104.     $context->restrictions['type'] = array_unique(array_merge($context->restrictions['type'], array_keys(array_filter($conf['type']))));
  105.   }
  106.   else {
  107.     $context->restrictions['type'] = array_keys(array_filter($conf['type']));
  108.   }
  109. }
  110.  
  111. /**
  112.  * Provide a summary description based upon the checked entity_bundle.
  113.  */
  114. function ctools_entity_bundle_ctools_access_summary($conf, $context, $plugin) {
  115.   if (!isset($conf['type'])) {
  116.     $conf['type'] = array();
  117.   }
  118.  
  119.   list($plugin_name, $entity_type) = explode(':', $plugin['name']);
  120.   if (!$entity_type) {
  121.     return t('Error, misconfigured entity_bundle access plugin');
  122.   };
  123.  
  124.   $entity = entity_get_info($entity_type);
  125.  
  126.   $names = array();
  127.   foreach (array_filter($conf['type']) as $type) {
  128.     $names[] = check_plain($entity['bundles'][$type]['label']);
  129.   }
  130.  
  131.   if (empty($names)) {
  132.     return t('@identifier is any bundle', array('@identifier' => $context->identifier));
  133.   }
  134.  
  135.   return format_plural(count($names), '@identifier is bundle "@types"', '@identifier bundle is one of "@types"', array('@types' => implode(', ', $names), '@identifier' => $context->identifier));
  136. }
  137.  
Parsed in 0.127 seconds