| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 | <?php/** * @file * Plugin to provide access control based upon entity bundle. *//** * Plugins are described by creating a $plugin array which will be used * by the system that includes this file. */$plugin = array(  'title' => t("Entity: bundle"),  'description' => t('Control access by entity bundle.'),  'callback' => 'ctools_entity_bundle_ctools_access_check',  'default' => array('type' => array()),  'settings form' => 'ctools_entity_bundle_ctools_access_settings',  'settings form submit' => 'ctools_entity_bundle_ctools_access_settings_submit',  'summary' => 'ctools_entity_bundle_ctools_access_summary',  'restrictions' => 'ctools_entity_bundle_ctools_access_restrictions',  'get child' => 'ctools_entity_bundle_ctools_access_get_child',  'get children' => 'ctools_entity_bundle_ctools_access_get_children',);function ctools_entity_bundle_ctools_access_get_child($plugin, $parent, $child) {  $plugins = ctools_entity_bundle_ctools_access_get_children($plugin, $parent);  return $plugins[$parent . ':' . $child];}function ctools_entity_bundle_ctools_access_get_children($plugin, $parent) {  $entities = entity_get_info();  $plugins = array();  foreach ($entities as $entity_type => $entity) {    $plugin['title'] = t('@entity: Bundle', array('@entity' => $entity['label']));    $plugin['keyword'] = $entity_type;    $plugin['description'] = t('Control access by @entity entity bundle.', array('@entity' => $entity_type));    $plugin['name'] = $parent . ':' . $entity_type;    $plugin['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type);    $plugins[$parent . ':' . $entity_type] = $plugin;  }  return $plugins;}/** * Settings form for the 'by entity_bundle' access plugin */function ctools_entity_bundle_ctools_access_settings($form, &$form_state, $conf) {  $plugin = $form_state['plugin'];  $entity_type = explode(':', $plugin['name']);  $entity_type = $entity_type[1];  $entity = entity_get_info($entity_type);  foreach ($entity['bundles'] as $type => $info) {    $options[$type] = check_plain($info['label']);  }  $form['settings']['type'] = array(    '#title' => t('Entity Bundle'),    '#type' => 'checkboxes',    '#options' => $options,    '#description' => t('Only the checked entity bundles will be valid.'),    '#default_value' => $conf['type'],  );  return $form;}/** * Compress the entity bundles allowed to the minimum. */function ctools_entity_bundle_ctools_access_settings_submit($form, &$form_state) {  $form_state['values']['settings']['type'] = array_filter($form_state['values']['settings']['type']);}/** * Check for access. */function ctools_entity_bundle_ctools_access_check($conf, $context, $plugin) {  list($plugin_name, $entity_type) = explode(':', $plugin['name']);  if (!$entity_type) {    return FALSE;  };  $entity = entity_get_info($entity_type);  // As far as I know there should always be a context at this point, but this  // is safe.  if (empty($context) || empty($context->data) || empty($context->data->{$entity['entity keys']['bundle']})) {    return FALSE;  }  if (array_filter($conf['type']) && empty($conf['type'][$context->data->{$entity['entity keys']['bundle']}])) {    return FALSE;  }  return TRUE;}/** * Inform the UI that we've eliminated a bunch of possibilities for this * context. */function ctools_entity_bundle_ctools_access_restrictions($conf, &$context) {  if (isset($context->restrictions['type'])) {    $context->restrictions['type'] = array_unique(array_merge($context->restrictions['type'], array_keys(array_filter($conf['type']))));  }  else {    $context->restrictions['type'] = array_keys(array_filter($conf['type']));  }}/** * Provide a summary description based upon the checked entity_bundle. */function ctools_entity_bundle_ctools_access_summary($conf, $context, $plugin) {  if (!isset($conf['type'])) {    $conf['type'] = array();  }  list($plugin_name, $entity_type) = explode(':', $plugin['name']);  if (!$entity_type) {    return t('Error, misconfigured entity_bundle access plugin');  };  $entity = entity_get_info($entity_type);  $names = array();  foreach (array_filter($conf['type']) as $type) {    $names[] = check_plain($entity['bundles'][$type]['label']);  }  if (empty($names)) {    return t('@identifier is any bundle', array('@identifier' => $context->identifier));  }  return format_plural(count($names), '@identifier is bundle "@types"', '@identifier bundle is one of "@types"', array('@types' => implode(', ', $names), '@identifier' => $context->identifier));}
 |