Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.94 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Implements hook_block_info().
  5.  */
  6. function relation_entity_collector_block_info() {
  7.   return array(
  8.     'block' => array(
  9.       'info' => t('Relation Entity Collector'),
  10.     ),
  11.   );
  12. }
  13.  
  14. /**
  15.  * Implements hook_block_view().
  16.  */
  17. function relation_entity_collector_block_view() {
  18.   $block['subject'] = t('Entity Collector');
  19.   $block['content']['#pre_render'] = array('relation_entity_collector_pre_render');
  20.   return $block;
  21. }
  22.  
  23. /**
  24.  * Pre render callback for the entity_collector block.
  25.  */
  26. function relation_entity_collector_pre_render($element) {
  27.   $element['form'] = drupal_get_form('relation_entity_collector');
  28.   return $element;
  29. }
  30.  
  31. /**
  32.  * Implements hook_entity_load().
  33.  */
  34. function relation_entity_collector_entity_load($entities, $type) {
  35.   $entities_store = &drupal_static('relation_entities', array());
  36.   $enabled = &drupal_static(__FUNCTION__);
  37.   // Recursion protection.
  38.   if ($enabled === -1) {
  39.     return;
  40.   }
  41.   if (!isset($enabled)) {
  42.     $enabled = -1;
  43.     // This can fire entity_load() so we need to protect from recursion: will
  44.     // try to get the theme from the menu which will, in turn, load the objects
  45.     // in the router path.
  46.     drupal_theme_initialize();
  47.     $block_info = _block_load_blocks();
  48.     $enabled = FALSE;
  49.     foreach ($block_info as $region => $blocks) {
  50.       if (isset($blocks['relation_entity_collector_block'])) {
  51.         $enabled = TRUE;
  52.         break;
  53.       }
  54.     }
  55.   }
  56.   if ($enabled) {
  57.     $entities_store += array($type => array());
  58.     $entities_store[$type] += $entities;
  59.   }
  60. }
  61.  
  62. /**
  63.  * The entity_collector form.
  64.  */
  65. function relation_entity_collector($form, &$form_state) {
  66.   $form['#attached']['css'] = array(
  67.     drupal_get_path('module', 'relation_entity_collector') . '/relation_entity_collector.css',
  68.   );
  69.   $types = relation_get_types();
  70.   if (empty($types)) {
  71.     $form['explanation']['#markup'] = t('Before you can create relations, you need to create one or more !link. Once you\'ve done that, visit any page that loads one or more entities, and use this block to add entities to a new relation. Picked entities stay in the entity_collector until cleared or a relation is created so it is possible to collect the entities from several pages.', array( '!link' => l('relation types', 'admin/structure/relation')));
  72.     return $form;
  73.   }
  74.   if ($relation_entities = drupal_static('relation_entities', array())) {
  75.     foreach ($relation_entities as $entity_type => $entities) {
  76.       foreach ($entities as $entity_id => $entity) {
  77.         list( , , $entity_bundle) = entity_extract_ids($entity_type, $entity);
  78.         $options["$entity_type:$entity_id"] = "$entity_bundle: " . entity_label($entity_type, $entity);
  79.       }
  80.     }
  81.     $form_state['relation_entities_options'] = $options;
  82.   }
  83.   if (empty($form_state['relation_entities_options'])) {
  84.     $form['explanation']['#markup'] = t('This block shows all loaded entities on a page and allows adding them to a relation. Please navigate to a page where entities are loaded. Entities picked stay in the entity_collector until cleared or a relation is created so it is possible to collect the entities from several pages.');
  85.     return $form;
  86.   }
  87.   $relation_types = array();
  88.   foreach ($types as $type) {
  89.     $relation_types[$type->relation_type] = $type->label;
  90.   }
  91.   $form['relation_type'] = array(
  92.     '#type'          => 'select',
  93.     '#title'         => t('Relation type'),
  94.     '#default_value' => isset($_SESSION['relation_type']) ? $_SESSION['relation_type'] : '',
  95.     '#options'       => $relation_types,
  96.     '#empty_value'   => '',
  97.     '#empty_option'  => t('Select a relation type'),
  98.   );
  99.   $form['entity_key'] = array(
  100.     '#type'           => 'select',
  101.     '#title'          => t('Entity'),
  102.     '#options'        => $form_state['relation_entities_options'],
  103.     '#default_value'  => '',
  104.     '#empty_value'    => '',
  105.     '#empty_option'   => t('Select an entity'),
  106.   );
  107.   $form['pick'] = array(
  108.     '#type' => 'submit',
  109.     '#value' => t('Pick'),
  110.     '#submit' => array('relation_entity_collector_pick'),
  111.     '#ajax'  => array(
  112.       'wrapper' => 'relation_entity_collector_reload',
  113.       'callback' => '_relation_entity_collector_ajax',
  114.     ),
  115.   );
  116.   $form['reload'] = array(
  117.     '#type' => 'fieldset',
  118.     '#title' => t('Picked entities'),
  119.   );
  120.   $form['reload']['#prefix'] = '<span id="relation_entity_collector_reload">';
  121.   $form['reload']['#suffix'] = '</span>';
  122.   if (!empty($_SESSION['relation_entity_keys'])) {
  123.     $form['reload']['stored'] = _relation_stored_entity_keys_list();
  124.     if (count($form['reload']['stored']['#items']) > 1) {
  125.       $form['reload']['save'] = array(
  126.         '#type' => 'submit',
  127.         '#value' => t('Create relation'),
  128.         '#submit' => array('relation_entity_collector_save'),
  129.       );
  130.     }
  131.     if (isset($_SESSION['relation_entity_keys'])) {
  132.       $form['reload']['clear'] = array(
  133.         '#type' => 'submit',
  134.         '#value' => t('Clear'),
  135.         '#submit' => array('relation_entity_collector_clear'),
  136.         '#ajax'  => array(
  137.           'wrapper' => 'relation_entity_collector_reload',
  138.           'callback' => '_relation_entity_collector_ajax',
  139.         ),
  140.       );
  141.     }
  142.   }
  143.   $form['explanation'] = array(
  144.     '#prefix' => '<div id=\'relation-entity-collector-explanation\'>',
  145.     '#markup' => t('This block shows all loaded entities on the page. Entities picked stay in the Entity Collector until cleared or a relation is created so it is possible to collect the entities from several pages.'),
  146.     '#suffix' => '</div>',
  147.   );
  148.   return $form;
  149. }
  150.  
  151. /**
  152.  * Trivial AJAX helper.
  153.  */
  154. function _relation_entity_collector_ajax($form) {
  155.   return $form['reload'];
  156. }
  157.  
  158. /**
  159.  * Helper to get a item_list render structure out of the entities in session.
  160.  */
  161. function _relation_stored_entity_keys_list() {
  162.   $list = array();
  163.   foreach ($_SESSION['relation_entity_keys'] as $entity_key) {
  164.     // The structure is (entity_type, entity_id, entity label).
  165.     $list[] = $entity_key['entity_key'];
  166.   }
  167.   return array(
  168.     '#theme' => 'item_list',
  169.     '#items' => $list,
  170.   );
  171. }
  172.  
  173. /**
  174.  * Submit handler for the pick button.
  175.  */
  176. function relation_entity_collector_pick($form, &$form_state) {
  177.   $_SESSION['relation_entity_keys'][] = $form_state['pick'];
  178.   $_SESSION['relation_type'] = $form_state['values']['relation_type'];
  179.   $form_state['rebuild'] = TRUE;
  180. }
  181.  
  182. /**
  183.  * Validate form submission for the entity_collector.
  184.  */
  185. function relation_entity_collector_validate($form, &$form_state) {
  186.   $errors = array();
  187.   $current = array();
  188.   $in_progress = FALSE;
  189.  
  190.   if ($form_state['clicked_button']['#value'] == t('Pick')) {
  191.     // Require values.
  192.     $relation_type = $form_state['values']['relation_type'];
  193.     $entity_key = $form_state['values']['entity_key'];
  194.     $errors[] = empty($relation_type) ? array('relation_type', t('Please select a relation type.')) : NULL;
  195.     $errors[] = empty($entity_key) ? array('entity_key', t('Please select an entity.')) : NULL;
  196.     if (count(array_filter($errors))) {
  197.       foreach ($errors as $error) {
  198.         form_set_error($error[0], $error[1]);
  199.       }
  200.       return;
  201.     }
  202.    
  203.     // Here we get (entity_type, entity_id).
  204.     $break = explode(':', $entity_key);
  205.     // Add the label for later display. #options is check_plain'd but we need
  206.     // to do that ourselves.
  207.     $entity_key = check_plain($form['entity_key']['#options'][$entity_key]);
  208.     $entity_key_array = explode(':', $entity_key, 2);
  209.     // Indexes are added in ascending order, starting from 0.
  210.     $_SESSION += array('relation_entity_keys' => array());
  211.     $next_index = count($_SESSION['relation_entity_keys']);
  212.     // If validation succeeds we will add this in the submit handler.
  213.     $form_state['pick'] = array(
  214.       'entity_type' => $break[0],
  215.       'entity_id' => $break[1],
  216.       'entity_bundle' => $entity_key_array[0],
  217.       'r_index' => $next_index,
  218.       'entity_key' => $entity_key,
  219.     );
  220.     $current[] = $form_state['pick'];
  221.     $in_progress = TRUE;
  222.   }
  223. }
  224.  
  225. /**
  226.  * Submit handler for the save button.
  227.  */
  228. function relation_entity_collector_save($form, $form_state) {
  229.   if (isset($_SESSION['relation_type'])) {
  230.     $relation = relation_create($_SESSION['relation_type'], $_SESSION['relation_entity_keys']);
  231.     $rid = relation_save($relation);
  232.     if ($rid) {
  233.       $link = l($_SESSION['relation_type'], "relation/$rid");
  234.       $list = _relation_stored_entity_keys_list();
  235.       $rendered_list = drupal_render($list);
  236.       $message = t('Created new !link from !list', array('!link' => $link, '!list' => $rendered_list));
  237.       drupal_set_message($message);
  238.       relation_entity_collector_clear($form, $form_state);
  239.     }
  240.     else {
  241.       drupal_set_message('Relation not created', 'error');
  242.     }
  243.   }
  244. }
  245.  
  246. /**
  247.  * Submit handler for the clear button.
  248.  */
  249. function relation_entity_collector_clear($form, &$form_state) {
  250.   unset($_SESSION['relation_type'], $_SESSION['relation_entity_keys']);
  251.   $form_state['rebuild'] = TRUE;
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement