pastebin

Paste Search Dynamic
Recent pastes
Entity id
  1. <?php
  2.  
  3. /**
  4.  * @file
  5.  *
  6.  * Plugin to provide an argument handler for all entity ids.
  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: ID"),
  15.   'description' => t('Creates an entity context from an entity ID argument.'),
  16.   'context' => 'ctools_argument_entity_id_context',
  17.   'get child' => 'ctools_argument_entity_id_get_child',
  18.   'get children' => 'ctools_argument_entity_id_get_children',
  19.   'default' => array(
  20.     'entity_id' => '',
  21.   ),
  22.   'placeholder form' => 'ctools_argument_entity_id_ctools_argument_placeholder',
  23. );
  24.  
  25. function ctools_argument_entity_id_get_child($plugin, $parent, $child) {
  26.   $plugins = ctools_argument_entity_id_get_children($plugin, $parent);
  27.   return $plugins[$parent . ':' . $child];
  28. }
  29.  
  30. function ctools_argument_entity_id_get_children($original_plugin, $parent) {
  31.   $entities = entity_get_info();
  32.   $plugins = array();
  33.   foreach ($entities as $entity_type => $entity) {
  34.     $plugin = $original_plugin;
  35.     $plugin['title'] = t('@entity: ID', array('@entity' => $entity['label']));
  36.     $plugin['keyword'] = $entity_type;
  37.     $plugin['description'] = t('Creates @entity context from an ID argument.', array('@entity' => $entity_type));
  38.     $plugin['name'] = $parent . ':' . $entity_type;
  39.     $plugin_id = $parent . ':' . $entity_type;
  40.     drupal_alter('ctools_entity_context', $plugin, $entity, $plugin_id);
  41.     $plugins[$plugin_id] = $plugin;
  42.   }
  43.   drupal_alter('ctools_entity_contexts', $plugins);
  44.  
  45.   return $plugins;
  46. }
  47.  
  48. /**
  49.  * Discover if this argument gives us the entity we crave.
  50.  */
  51. function ctools_argument_entity_id_context($arg = null, $conf = null, $empty = false) {
  52.   $entity_type = explode(':', $conf['name']);
  53.   $entity_type = $entity_type[1];
  54.   // If unset it wants a generic, unfilled context.
  55.   if ($empty) {
  56.     return ctools_context_create_empty('entity:' . $entity_type);
  57.   }
  58.  
  59.   // We can accept either an entity object or a pure id.
  60.   if (is_object($arg)) {
  61.     return ctools_context_create('entity:' . $entity_type, $arg);
  62.   }
  63.  
  64.   // Trim spaces and other garbage.
  65.   $arg = trim($arg);
  66.  
  67.   if (!is_numeric($arg)) {
  68.     $preg_matches = array();
  69.     $match = preg_match('/[id: (d+)]/', $arg, $preg_matches);
  70.     if (!$match) {
  71.       $match = preg_match('/^id: (d+)/', $arg, $preg_matches);
  72.     }
  73.  
  74.     if ($match) {
  75.       $id = $preg_matches[1];
  76.     }
  77.     if (isset($id) && is_numeric($id)) {
  78.       return ctools_context_create('entity:' . $entity_type, $id);
  79.     }
  80.     return false;
  81.   }
  82.  
  83.   $entities = entity_load($entity_type, array($arg));
  84.   if (empty($entities)) {
  85.     return false;
  86.   }
  87.  
  88.   return ctools_context_create('entity:' . $entity_type, reset($entities));
  89. }
  90.  
  91. function ctools_argument_entity_id_settings_form(&$form, &$form_state, $conf) {
  92.   $plugin = &$form_state['plugin'];
  93.  
  94.   $form['settings']['entity'] = array(
  95.     '#title' => t('Enter the title or ID of a @entity entity', array('@entity' => $plugin['keyword'])),
  96.     '#type' => 'textfield',
  97.     '#maxlength' => 512,
  98.     '#autocomplete_path' => 'ctools/autocomplete/' . $plugin['keyword'],
  99.     '#weight' => -10,
  100.   );
  101.  
  102.   if (!empty($conf['entity_id'])) {
  103.     $info = entity_load($plugin['keyword'], array($conf['entity_id']));
  104.     $info = $info[$conf['entity_id']];
  105.     if ($info) {
  106.       $entity = entity_get_info($plugin['keyword']);
  107.       $uri = entity_uri($plugin['keyword'], $info);
  108.       if (is_array($uri) && $entity['entity keys']['label']) {
  109.         $link = l(t("'%title' [%type id %id]", array('%title' => $info->{$entity['entity keys']['label']}, '%type' => $plugin['keyword'], '%id' => $conf['entity_id'])), $uri['path'], array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => true));
  110.       }
  111.       elseif (is_array($uri)) {
  112.         $link = l(t("[%type id %id]", array('%type' => $plugin['keyword'], '%id' => $conf['entity_id'])), $uri['path'], array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => true));
  113.       }
  114.       elseif ($entity['entity keys']['label']) {
  115.         $link = l(t("'%title' [%type id %id]", array('%title' => $info->{$entity['entity keys']['label']}, '%type' => $plugin['keyword'], '%id' => $conf['entity_id'])), file_create_url($uri), array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => true));
  116.       }
  117.       else {
  118.         $link = t("[%type id %id]", array('%type' => $plugin['keyword'], '%id' => $conf['entity_id']));
  119.       }
  120.       $form['settings']['entity']['#description'] = t('Currently set to !link', array('!link' => $link));
  121.     }
  122.   }
  123.  
  124.   $form['settings']['entity_id'] = array(
  125.     '#type' => 'value',
  126.     '#value' => isset($conf['entity_id']) ? $conf['entity_id'] : '',
  127.   );
  128.  
  129.   $form['settings']['entity_type'] = array(
  130.     '#type' => 'value',
  131.     '#value' => $plugin['keyword'],
  132.   );
  133.  
  134.   return $form;
  135. }
  136.  
  137. function ctools_argument_entity_id_ctools_argument_placeholder($conf) {
  138.   $conf = array(
  139.     '#title' => t('Enter the title or ID of a @entity entity', array('@entity' => $conf['keyword'])),
  140.     '#type' => 'textfield',
  141.     '#maxlength' => 512,
  142.     '#autocomplete_path' => 'ctools/autocomplete/' . $conf['keyword'],
  143.     '#weight' => -10,
  144.   );
  145.  
  146.   return $conf;
  147. }
  148. ?>
Parsed in 0.160 seconds