You are here

public function EntityCollector::buildForm in Relation 8

Same name and namespace in other branches
  1. 8.2 relation_entity_collector/src/Form/EntityCollector.php \Drupal\relation_entity_collector\Form\EntityCollector::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

relation_entity_collector/src/Form/EntityCollector.php, line 31
Contains \Drupal\relation_entity_collector\Form\EntityCollector.

Class

EntityCollector
Provides a entity collector form.

Namespace

Drupal\relation_entity_collector\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $storage =& $form_state
    ->getStorage();
  $form['#attached'] = array(
    'library' => array(
      'relation_entity_controller/drupal.relation_entity_controller',
    ),
  );
  $relation_types_options = relation_get_relation_types_options();
  if (empty($relation_types_options)) {
    $form['explanation']['#markup'] = $this
      ->t('Before you can create relations, you need to create one or more <a href="@url">relation types</a>. 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(
      '@url' => $this
        ->url('entity.relation_type.collection'),
    ));
    return $form;
  }
  $relation_type = isset($_SESSION['relation_type']) ? $_SESSION['relation_type'] : '';

  // Forget the selected relation type if it's no longer available.
  if (!isset($relation_types_options[$relation_type])) {
    unset($_SESSION['relation_type']);
    $relation_type = '';
  }
  if ($relation_entities = drupal_static('relation_entities', array())) {
    $options = array();
    foreach ($relation_entities as $entity_type => $entities) {
      foreach ($entities as $entity_id => $entity) {
        $entity_bundle = $entity
          ->bundle();
        if ($relation_type) {
          $relation_type_object = RelationType::load($relation_type);
          $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;
              }
            }
          }
        }
        else {
          $valid = TRUE;
        }
        if ($valid) {
          $bundles = \Drupal::service('entity_type.bundle.info')
            ->getBundleInfo($entity_type);
          $options["{$entity_type}:{$entity_id}"] = $bundles[$entity_bundle]['label'] . ': ' . $entity
            ->label();
        }
      }
    }
    asort($options);
    $storage['relation_entities_options'] = $options;
    $form_state
      ->setStorage($storage);
  }
  if (empty($storage['relation_entities_options'])) {
    $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.');
    return $form;
  }
  $form['relation_type'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Relation type'),
    '#default_value' => $relation_type,
    '#options' => $relation_types_options,
    '#empty_value' => '',
    '#empty_option' => $this
      ->t('Select a relation type'),
    '#access' => empty($_SESSION['relation_edit']),
  );
  $form['entity_key'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Select an entity'),
    '#options' => $storage['relation_entities_options'],
    '#default_value' => '',
    '#description' => $this
      ->t('Selector shows all !entities loaded on this page.', array(
      '!entities' => $this
        ->l($this
        ->t('entities'), Url::fromUri('http://drupal.org/glossary#entity')),
    )),
  );
  $form['pick'] = array(
    '#type' => 'submit',
    '#value' => $this
      ->t('Pick'),
    '#submit' => array(
      'relation_entity_collector_pick',
    ),
    '#ajax' => array(
      'wrapper' => 'relation_entity_collector_reload',
      'callback' => '_relation_entity_collector_ajax',
    ),
  );
  $form['reload'] = array(
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Picked entities'),
  );
  $form['reload']['#prefix'] = '<span id="relation_entity_collector_reload">';
  $form['reload']['#suffix'] = '</span>';
  if (!empty($_SESSION['relation_entity_keys'])) {
    $form['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['reload']['table']['weight'][] = array(
        '#type' => 'weight',
        '#delta' => count($_SESSION['relation_entity_keys']),
        '#default_value' => $delta,
        '#title_display' => 'invisible',
        '#title' => '',
      );
      $form['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_reload',
          'callback' => '_relation_entity_collector_ajax',
        ),
      );
      $form['reload']['table']['#tree'] = TRUE;
      $form['reload']['table']['#theme'] = 'relation_entity_collector_table';
    }
    if (!isset($relation_type_object) && !empty($relation_type)) {
      $relation_type_object = RelationType::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['reload']['save'] = array(
        '#type' => 'submit',
        '#value' => t('Save relation'),
        '#submit' => array(
          'relation_entity_collector_save',
        ),
      );
    }
    if (isset($_SESSION['relation_entity_keys'])) {
      $form['reload']['clear'] = array(
        '#type' => 'submit',
        '#value' => t('Clear'),
        '#submit' => array(
          'relation_entity_collector_clear',
        ),
        '#ajax' => array(
          'wrapper' => 'relation_entity_collector_reload',
          'callback' => '_relation_entity_collector_ajax',
        ),
      );
    }
  }
  $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;
}