You are here

public function EntityQueueForm::form in Entityqueue 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/EntityQueueForm.php, line 80

Class

EntityQueueForm
Base form for entity queue edit forms.

Namespace

Drupal\entityqueue\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $queue = $this->entity;
  $form['#title'] = $this
    ->t('Configure <em>@queue</em> entity queue', [
    '@queue' => $queue
      ->label(),
  ]);

  // Default to nodes as the queue target entity type.
  $target_entity_type_id = $queue
    ->getTargetEntityTypeId() ?: 'node';
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Name'),
    '#maxlength' => 255,
    '#size' => 80,
    '#default_value' => $queue
      ->label(),
    '#description' => $this
      ->t('The human-readable name of this entity queue. This name must be unique.'),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $queue
      ->id(),
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    '#machine_name' => [
      'exists' => '\\Drupal\\entityqueue\\Entity\\EntityQueue::load',
    ],
    '#disabled' => !$queue
      ->isNew(),
  ];
  $handler_plugin = $this
    ->getHandlerPlugin($queue, $form_state);
  $form['handler'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Queue type'),
    '#options' => $this->entityQueueHandlerManager
      ->getAllEntityQueueHandlers(),
    '#default_value' => $handler_plugin
      ->getPluginId(),
    '#required' => TRUE,
    '#disabled' => !$queue
      ->isNew(),
    '#ajax' => [
      'callback' => '::settingsAjax',
      'wrapper' => 'entityqueue-handler-settings-wrapper',
      'trigger_as' => [
        'name' => 'handler_change',
      ],
    ],
  ];
  foreach ($this->entityQueueHandlerManager
    ->getDefinitions() as $handler_id => $definition) {
    if (!empty($definition['description'])) {
      $form['handler'][$handler_id]['#description'] = $definition['description'];
    }
  }
  $form['handler_change'] = [
    '#type' => 'submit',
    '#name' => 'handler_change',
    '#value' => $this
      ->t('Change type'),
    '#limit_validation_errors' => [],
    '#submit' => [
      [
        get_called_class(),
        'settingsAjaxSubmit',
      ],
    ],
    '#attributes' => [
      'class' => [
        'js-hide',
      ],
    ],
    '#ajax' => [
      'callback' => '::settingsAjax',
      'wrapper' => 'entityqueue-handler-settings-wrapper',
    ],
  ];
  $form['handler_settings_wrapper'] = [
    '#type' => 'container',
    '#id' => 'entityqueue-handler-settings-wrapper',
    '#tree' => TRUE,
  ];
  $form['handler_settings_wrapper']['handler_settings'] = [];
  $subform_state = SubformState::createForSubform($form['handler_settings_wrapper']['handler_settings'], $form, $form_state);
  if ($handler_settings = $handler_plugin
    ->buildConfigurationForm($form['handler_settings_wrapper']['handler_settings'], $subform_state)) {
    $form['handler_settings_wrapper']['handler_settings'] = $handler_settings + [
      '#type' => 'details',
      '#title' => $this
        ->t('@handler settings', [
        '@handler' => $handler_plugin
          ->getPluginDefinition()['title'],
      ]),
      '#open' => TRUE,
    ];
  }
  $form['settings'] = [
    '#type' => 'vertical_tabs',
  ];
  $form['queue_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Queue settings'),
    '#open' => TRUE,
    '#tree' => TRUE,
    '#group' => 'settings',
  ];
  $form['queue_settings']['size'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'form--inline',
        'clearfix',
      ],
    ],
    '#process' => [
      [
        EntityReferenceItem::class,
        'formProcessMergeParent',
      ],
    ],
  ];
  $form['queue_settings']['size']['min_size'] = [
    '#type' => 'number',
    '#size' => 2,
    '#default_value' => $queue
      ->getMinimumSize(),
    '#field_prefix' => $this
      ->t('Restrict this queue to a minimum of'),
  ];
  $form['queue_settings']['size']['max_size'] = [
    '#type' => 'number',
    '#default_value' => $queue
      ->getMaximumSize(),
    '#field_prefix' => $this
      ->t('and a maximum of'),
    '#field_suffix' => $this
      ->t('items.'),
  ];
  $form['queue_settings']['act_as_queue'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Act as queue'),
    '#default_value' => $queue
      ->getActAsQueue(),
    '#description' => $this
      ->t('When enabled, adding more than the maximum number of items will remove extra items from the queue.'),
    '#states' => [
      'invisible' => [
        ':input[name="queue_settings[max_size]"]' => [
          'value' => 0,
        ],
      ],
    ],
  ];
  $form['queue_settings']['reverse'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Reverse'),
    '#default_value' => $queue
      ->isReversed(),
    '#description' => $this
      ->t('By default, new items are added to the bottom of the queue. If this option is checked, new items will be added to the top of the queue.'),
  ];

  // We have to duplicate all the code from
  // \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::fieldSettingsForm()
  // because field settings forms are not easily embeddable.
  $form['entity_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Entity settings'),
    '#open' => TRUE,
    '#tree' => TRUE,
    '#group' => 'settings',
    '#weight' => -1,
  ];

  // Get all selection plugins for this entity type.
  $selection_plugins = $this->selectionManager
    ->getSelectionGroups($target_entity_type_id);
  $selection_handlers_options = [];
  foreach (array_keys($selection_plugins) as $selection_group_id) {

    // We only display base plugins (e.g. 'default', 'views', ...) and not
    // entity type specific plugins (e.g. 'default:node', 'default:user',
    // ...).
    if (array_key_exists($selection_group_id, $selection_plugins[$selection_group_id])) {
      $selection_handlers_options[$selection_group_id] = Html::escape($selection_plugins[$selection_group_id][$selection_group_id]['label']);
    }
    elseif (array_key_exists($selection_group_id . ':' . $target_entity_type_id, $selection_plugins[$selection_group_id])) {
      $selection_group_plugin = $selection_group_id . ':' . $target_entity_type_id;
      $selection_handlers_options[$selection_group_plugin] = Html::escape($selection_plugins[$selection_group_id][$selection_group_plugin]['base_plugin_label']);
    }
  }
  ksort($selection_handlers_options);
  $form['entity_settings']['settings'] = [
    '#type' => 'container',
    '#process' => [
      [
        EntityReferenceItem::class,
        'fieldSettingsAjaxProcess',
      ],
      [
        EntityReferenceItem::class,
        'formProcessMergeParent',
      ],
    ],
    '#element_validate' => [
      [
        get_class($this),
        'entityReferenceSelectionSettingsValidate',
      ],
    ],
  ];

  // @todo It should be up to the queue handler to determine what entity types
  //   are queue-able.
  $form['entity_settings']['settings']['target_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Type of items to queue'),
    '#options' => $this->entityTypeRepository
      ->getEntityTypeLabels(TRUE),
    '#default_value' => $target_entity_type_id,
    '#required' => TRUE,
    '#disabled' => !$queue
      ->isNew(),
    '#size' => 1,
    '#ajax' => TRUE,
    '#limit_validation_errors' => [],
  ];
  $form['entity_settings']['settings']['handler'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Reference method'),
    '#options' => $selection_handlers_options,
    '#default_value' => $queue
      ->getEntitySettings()['handler'],
    '#required' => TRUE,
    '#ajax' => TRUE,
    '#limit_validation_errors' => [],
  ];
  $form['entity_settings']['settings']['handler_submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Change handler'),
    '#limit_validation_errors' => [],
    '#attributes' => [
      'class' => [
        'js-hide',
      ],
    ],
    '#submit' => [
      [
        EntityReferenceItem::class,
        'settingsAjaxSubmit',
      ],
    ],
  ];
  $form['entity_settings']['settings']['handler_settings'] = [
    '#type' => 'container',
  ];
  $entity_settings = $queue
    ->getEntitySettings();
  $entity_settings += $entity_settings['handler_settings'];
  unset($entity_settings['handler_settings']);
  $selection_handler = $this->selectionManager
    ->getInstance($entity_settings);
  $form['entity_settings']['settings']['handler_settings'] += $selection_handler
    ->buildConfigurationForm([], $form_state);

  // For entityqueue's purposes, the 'target_bundles' setting of the 'default'
  // selection handler does not have to be required.
  if (isset($form['entity_settings']['settings']['handler_settings']['target_bundles'])) {
    $form['entity_settings']['settings']['handler_settings']['target_bundles']['#required'] = FALSE;
  }

  // Also, the 'auto-create' option is mostly useless and confusing in the
  // entityqueue UI.
  if (isset($form['entity_settings']['settings']['handler_settings']['auto_create'])) {
    $form['entity_settings']['settings']['handler_settings']['auto_create']['#access'] = FALSE;
  }
  return $form;
}