You are here

function relation_entity_collector in Relation 7

The entity_collector form.

3 string references to 'relation_entity_collector'
RelationEntityCollectorTestCase::setUp in relation_entity_collector/tests/relation_entity_collector.test
Sets up a Drupal site for running functional and integration tests.
RelationViewsTestCase::setUp in tests/relation.views.test
Sets up a Drupal site for running functional and integration tests.
relation_entity_collector_pre_render in relation_entity_collector/relation_entity_collector.module
Pre render callback for the entity_collector block.

File

relation_entity_collector/relation_entity_collector.module, line 119
Relation Entity Collector Block.

Code

function relation_entity_collector($form, &$form_state) {
  $form['#attached']['css'] = array(
    drupal_get_path('module', 'relation_entity_collector') . '/relation_entity_collector.css',
  );
  $types = relation_get_types();
  if (empty($types)) {
    $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' => module_exists('relation_ui') ? l(t('relation types'), 'admin/structure/relation') : t('relation types'),
    ));
    return $form;
  }

  // If nothing is picked, forget last relation type picked
  if (isset($_SESSION['relation_entity_keys']) && count($_SESSION['relation_entity_keys']) < 1) {
    unset($_SESSION['relation_type']);
  }
  $relation_types = array();
  foreach ($types as $type) {
    $relation_types[$type->relation_type] = $type->label;
  }

  // Picking entities in progress or not
  $relation_type = isset($_SESSION['relation_type']) ? $_SESSION['relation_type'] : '';

  // forget the selected relation type if it's no longer available
  if (!isset($relation_types[$relation_type])) {
    unset($_SESSION['relation_type']);
    $relation_type = '';
  }
  if (empty($relation_type)) {

    // User may have chosen a type, but not picked any entities yet
    if (isset($form_state['values']['relation_type'])) {
      $relation_type = $form_state['values']['relation_type'];
    }
    else {
      if (empty($relation_type) && count($types) == 1) {
        $relation_type = reset($types)->relation_type;
      }
    }
  }
  $relation_type_object = !empty($relation_type) ? relation_type_load($relation_type) : NULL;
  $options = array();
  $all_entity_cache = isset($form_state['all_entity_cache']) ? $form_state['all_entity_cache'] : array();
  if (!isset($form_state['all_entity_cache'])) {
    $form_state['all_entity_cache'] = array();
  }
  if ($relation_entities = drupal_static('relation_entities', array())) {
    $form_state['all_entity_cache'] += $relation_entities;
  }
  foreach ($form_state['all_entity_cache'] as $entity_type => $entities) {
    foreach ($entities as $entity_id => $entity) {
      list(, , $entity_bundle) = entity_extract_ids($entity_type, $entity);
      if (!is_null($relation_type_object)) {
        $valid = FALSE;
        foreach (array(
          'source_bundles',
          'target_bundles',
        ) as $property) {
          foreach ($relation_type_object->{$property} as $allowed_bundle) {
            if ($allowed_bundle == "{$entity_type}:{$entity_bundle}" || $allowed_bundle == "{$entity_type}:*") {
              $valid = TRUE;
              break 2;
            }
          }
        }
      }
      else {
        $valid = TRUE;
      }
      if ($valid) {
        $bundle_label = _relation_get_bundle_label($entity_type, $entity_bundle);
        $options["{$entity_type}:{$entity_id}"] = $bundle_label . ': ' . entity_label($entity_type, $entity);
      }
    }
  }
  asort($options);
  $entity_key_default = count($options) == 1 ? key($options) : '';
  $form['entity_picker'] = array(
    '#prefix' => '<span id="relation_entity_collector_picker">',
    '#suffix' => '</span>',
  );
  $form['entity_picker']['relation_type'] = array(
    '#prefix' => '<span id="relation_entity_collector_pick_type">',
    '#suffix' => '</span>',
    '#type' => 'select',
    '#title' => t('Relation type'),
    '#default_value' => $relation_type,
    '#options' => $relation_types,
    '#empty_value' => '',
    '#empty_option' => t('- Select a relation type -'),
    '#access' => empty($_SESSION['relation_edit']),
    '#ajax' => array(
      'event' => 'change',
      'callback' => '_relation_entity_collector_ajax_picker',
      'wrapper' => 'relation_entity_collector_picker',
    ),
  );
  if (!empty($_SESSION['relation_type']) && count($_SESSION['relation_entity_keys']) > 0) {
    $form['entity_picker']['relation_type']['#attributes'] = array(
      'disabled' => TRUE,
    );
  }
  $form['entity_picker']['entity_key'] = array(
    '#type' => 'select',
    '#title' => t('Select an entity'),
    '#options' => $options,
    '#default_value' => $entity_key_default,
    '#empty_value' => '',
    '#empty_option' => t('- Select an entity -'),
    '#description' => t('Selector shows all valid !entities loaded on this page.', array(
      '!entities' => l(t('entities'), 'http://drupal.org/glossary#entity', array(
        'absolute' => TRUE,
        'external' => TRUE,
      )),
    )),
  );
  $form['entity_picker']['pick'] = array(
    '#type' => 'submit',
    '#value' => t('Pick'),
    '#submit' => array(
      'relation_entity_collector_pick',
    ),
    '#ajax' => array(
      'wrapper' => 'relation_entity_collector_picker',
      'callback' => '_relation_entity_collector_ajax_picker',
    ),
  );
  $form['entity_picker']['reload'] = array(
    '#type' => 'fieldset',
    '#title' => t('Picked entities'),
  );
  if (!empty($_SESSION['relation_entity_keys'])) {
    $form['entity_picker']['reload']['table']['#entity_collector_columns'] = array(
      'weight',
      'remove',
    );
    foreach ($_SESSION['relation_entity_keys'] as $delta => $entity_key) {

      // The structure is (entity_type, entity_id, entity label).
      $form['entity_picker']['reload']['table']['weight'][] = array(
        '#type' => 'weight',
        '#delta' => count($_SESSION['relation_entity_keys']),
        '#default_value' => $delta,
        '#title_display' => 'invisible',
        '#title' => '',
      );
      $form['entity_picker']['reload']['table']['remove'][] = array(
        '#name' => 'remove-' . $entity_key['entity_key'],
        '#type' => 'submit',
        '#value' => t('Remove'),
        '#entity_key' => $entity_key,
        '#submit' => array(
          'relation_entity_collector_remove',
        ),
        '#ajax' => array(
          'wrapper' => 'relation_entity_collector_picker',
          'callback' => '_relation_entity_collector_ajax_picker',
        ),
      );
      $form['entity_picker']['reload']['table']['#tree'] = TRUE;
      $form['entity_picker']['reload']['table']['#theme'] = 'relation_entity_collector_table';
    }
    if (!isset($relation_type_object) && !empty($relation_type)) {
      $relation_type_object = relation_type_load($relation_type);
    }
    $min_arity = isset($relation_type_object->min_arity) ? $relation_type_object->min_arity : 1;
    if (count($_SESSION['relation_entity_keys']) >= $min_arity) {
      $form['entity_picker']['reload']['save'] = array(
        '#type' => 'submit',
        '#value' => t('Save relation'),
        '#submit' => array(
          'relation_entity_collector_save',
        ),
      );
    }
    if (isset($_SESSION['relation_entity_keys'])) {
      $form['entity_picker']['reload']['clear'] = array(
        '#type' => 'submit',
        '#value' => t('Clear'),
        '#submit' => array(
          'relation_entity_collector_clear',
        ),
        '#ajax' => array(
          'wrapper' => 'relation_entity_collector_picker',
          'callback' => '_relation_entity_collector_ajax_picker',
        ),
      );
    }
  }
  $form['explanation'] = array(
    '#prefix' => '<div id=\'relation-entity-collector-explanation\'>',
    '#markup' => t('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.'),
    '#suffix' => '</div>',
  );
  return $form;
}