You are here

public function OverviewForm::buildForm in Linkit 8.5

Same name and namespace in other branches
  1. 8.4 src/Form/Matcher/OverviewForm.php \Drupal\linkit\Form\Matcher\OverviewForm::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

src/Form/Matcher/OverviewForm.php, line 61

Class

OverviewForm
Provides an overview form for matchers on a profile.

Namespace

Drupal\linkit\Form\Matcher

Code

public function buildForm(array $form, FormStateInterface $form_state, ProfileInterface $linkit_profile = NULL) {
  $this->linkitProfile = $linkit_profile;
  $form['#attached']['library'][] = 'linkit/linkit.admin';
  $form['plugins'] = [
    '#type' => 'table',
    '#header' => [
      [
        'data' => $this
          ->t('Matchers'),
        'colspan' => 2,
      ],
      $this
        ->t('Weight'),
      $this
        ->t('Operations'),
    ],
    '#empty' => $this
      ->t('No matchers added.'),
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'plugin-order-weight',
      ],
    ],
  ];
  foreach ($this->linkitProfile
    ->getMatchers() as $plugin) {
    $key = $plugin
      ->getUuid();
    $form['plugins'][$key]['#attributes']['class'][] = 'draggable';
    $form['plugins'][$key]['#weight'] = $plugin
      ->getWeight();
    $form['plugins'][$key]['label'] = [
      '#plain_text' => (string) $plugin
        ->getLabel(),
    ];
    $form['plugins'][$key]['summary'] = [];
    $summary = $plugin
      ->getSummary();
    if (!empty($summary)) {
      $form['plugins'][$key]['summary'] = [
        '#type' => 'inline_template',
        '#template' => '<div class="linkit-plugin-summary">{{ summary|safe_join("<br />") }}</div>',
        '#context' => [
          'summary' => $summary,
        ],
      ];
    }
    $form['plugins'][$key]['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight for @title', [
        '@title' => (string) $plugin
          ->getLabel(),
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $plugin
        ->getWeight(),
      '#attributes' => [
        'class' => [
          'plugin-order-weight',
        ],
      ],
    ];
    $form['plugins'][$key]['operations'] = [
      '#type' => 'operations',
      '#links' => [],
    ];
    $is_configurable = $plugin instanceof ConfigurableMatcherInterface;
    if ($is_configurable) {
      $form['plugins'][$key]['operations']['#links']['edit'] = [
        'title' => $this
          ->t('Edit'),
        'url' => Url::fromRoute('linkit.matcher.edit', [
          'linkit_profile' => $this->linkitProfile
            ->id(),
          'plugin_instance_id' => $key,
        ]),
      ];
    }
    $form['plugins'][$key]['operations']['#links']['delete'] = [
      'title' => $this
        ->t('Delete'),
      'url' => Url::fromRoute('linkit.matcher.delete', [
        'linkit_profile' => $this->linkitProfile
          ->id(),
        'plugin_instance_id' => $key,
      ]),
    ];
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    '#button_type' => 'primary',
  ];
  return $form;
}