You are here

public function OverviewForm::buildForm in Linkit 8.4

Same name in this branch
  1. 8.4 src/Form/Attribute/OverviewForm.php \Drupal\linkit\Form\Attribute\OverviewForm::buildForm()
  2. 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/Attribute/OverviewForm.php, line 66
Contains \Drupal\linkit\Form\Attribute\OverviewForm.

Class

OverviewForm
Provides an overview form for attribute on a profile.

Namespace

Drupal\linkit\Form\Attribute

Code

public function buildForm(array $form, FormStateInterface $form_state, ProfileInterface $linkit_profile = NULL) {
  $this->linkitProfile = $linkit_profile;
  $form['plugins'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Attribute'),
      $this
        ->t('Description'),
      $this
        ->t('Weight'),
      $this
        ->t('Operations'),
    ],
    '#empty' => $this
      ->t('No attributes added.'),
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'plugin-order-weight',
      ],
    ],
  ];
  foreach ($this->linkitProfile
    ->getAttributes() as $plugin) {
    $key = $plugin
      ->getPluginId();
    $form['plugins'][$key]['#attributes']['class'][] = 'draggable';
    $form['plugins'][$key]['#weight'] = $plugin
      ->getWeight();
    $form['plugins'][$key]['label'] = [
      '#plain_text' => (string) $plugin
        ->getLabel(),
    ];
    $form['plugins'][$key]['description'] = [
      '#plain_text' => (string) $plugin
        ->getDescription(),
    ];
    $form['plugins'][$key]['weight'] = [
      '#type' => 'weight',
      '#title' => 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 ConfigurableAttributeInterface;
    if ($is_configurable) {
      $form['plugins'][$key]['operations']['#links']['edit'] = [
        'title' => t('Edit'),
        'url' => Url::fromRoute('linkit.attribute.edit', [
          'linkit_profile' => $this->linkitProfile
            ->id(),
          'plugin_instance_id' => $key,
        ]),
      ];
    }
    $form['plugins'][$key]['operations']['#links']['delete'] = [
      'title' => t('Delete'),
      'url' => Url::fromRoute('linkit.attribute.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;
}