You are here

public function EntityMatcher::buildConfigurationForm in Linkit 8.5

Same name and namespace in other branches
  1. 8.4 src/Plugin/Linkit/Matcher/EntityMatcher.php \Drupal\linkit\Plugin\Linkit\Matcher\EntityMatcher::buildConfigurationForm()

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PluginFormInterface::buildConfigurationForm

4 calls to EntityMatcher::buildConfigurationForm()
FileMatcher::buildConfigurationForm in src/Plugin/Linkit/Matcher/FileMatcher.php
Form constructor.
NodeMatcher::buildConfigurationForm in src/Plugin/Linkit/Matcher/NodeMatcher.php
Form constructor.
TermMatcher::buildConfigurationForm in src/Plugin/Linkit/Matcher/TermMatcher.php
Form constructor.
UserMatcher::buildConfigurationForm in src/Plugin/Linkit/Matcher/UserMatcher.php
Form constructor.
4 methods override EntityMatcher::buildConfigurationForm()
FileMatcher::buildConfigurationForm in src/Plugin/Linkit/Matcher/FileMatcher.php
Form constructor.
NodeMatcher::buildConfigurationForm in src/Plugin/Linkit/Matcher/NodeMatcher.php
Form constructor.
TermMatcher::buildConfigurationForm in src/Plugin/Linkit/Matcher/TermMatcher.php
Form constructor.
UserMatcher::buildConfigurationForm in src/Plugin/Linkit/Matcher/UserMatcher.php
Form constructor.

File

src/Plugin/Linkit/Matcher/EntityMatcher.php, line 196

Class

EntityMatcher
Provides default linkit matchers for all entity types.

Namespace

Drupal\linkit\Plugin\Linkit\Matcher

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $entity_type = $this->entityTypeManager
    ->getDefinition($this->targetType);
  $form['metadata'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Suggestion metadata'),
    '#open' => TRUE,
    '#weight' => -100,
  ];
  $form['metadata']['metadata'] = [
    '#title' => $this
      ->t('Metadata'),
    '#type' => 'textfield',
    '#default_value' => $this->configuration['metadata'],
    '#description' => $this
      ->t('Metadata is shown together with each suggestion in the suggestion list.'),
    '#size' => 120,
    '#maxlength' => 255,
    '#weight' => 0,
  ];
  $this
    ->insertTokenList($form, [
    $this->targetType,
  ]);

  // Filter the possible bundles to use if the entity has bundles.
  if ($entity_type
    ->hasKey('bundle')) {
    $bundle_options = [];
    foreach ($this->entityTypeBundleInfo
      ->getBundleInfo($this->targetType) as $bundle_name => $bundle_info) {
      $bundle_options[$bundle_name] = $bundle_info['label'];
    }
    $form['bundle_restrictions'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Bundle restrictions'),
      '#open' => TRUE,
      '#weight' => -90,
    ];
    $form['bundle_restrictions']['bundles'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Restrict suggestions to the selected bundles'),
      '#options' => $bundle_options,
      '#default_value' => $this->configuration['bundles'],
      '#description' => $this
        ->t('If none of the checkboxes is checked, all bundles are allowed.'),
      '#element_validate' => [
        [
          get_class($this),
          'elementValidateFilter',
        ],
      ],
    ];
    $form['bundle_grouping'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Bundle grouping'),
      '#open' => TRUE,
    ];

    // Group the suggestions by bundle.
    $form['bundle_grouping']['group_by_bundle'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Group by bundle'),
      '#default_value' => $this->configuration['group_by_bundle'],
      '#description' => $this
        ->t('Group suggestions by their bundle.'),
    ];
  }
  $substitution_options = $this->substitutionManager
    ->getApplicablePluginsOptionList($this->targetType);
  $form['substitution'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('URL substitution'),
    '#open' => TRUE,
    '#weight' => 100,
    '#access' => count($substitution_options) !== 1,
  ];
  $form['substitution']['substitution_type'] = [
    '#title' => $this
      ->t('Substitution Type'),
    '#type' => 'select',
    '#default_value' => $this->configuration['substitution_type'],
    '#options' => $substitution_options,
    '#description' => $this
      ->t('Configure how the selected entity should be transformed into a URL for insertion.'),
  ];
  $form['limit'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Limit'),
    '#open' => TRUE,
  ];
  $form['limit']['limit'] = [
    '#type' => 'select',
    '#options' => [
      0 => $this
        ->t('Unlimited'),
      20 => 20,
      50 => 50,
      100 => 100,
      200 => 200,
    ],
    '#title' => $this
      ->t('Limit search results'),
    '#description' => $this
      ->t('Limit the amount of results displayed when searching.'),
    '#default_value' => $this->configuration['limit'],
  ];
  return $form;
}