You are here

public function ReferenceSettingsLinksForm::buildForm in Bibliography & Citation 8

Same name and namespace in other branches
  1. 2.0.x modules/bibcite_entity/src/Form/ReferenceSettingsLinksForm.php \Drupal\bibcite_entity\Form\ReferenceSettingsLinksForm::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 ConfigFormBase::buildForm

File

modules/bibcite_entity/src/Form/ReferenceSettingsLinksForm.php, line 64

Class

ReferenceSettingsLinksForm
Common Reference settings.

Namespace

Drupal\bibcite_entity\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('bibcite_entity.reference.settings');
  $links = $config
    ->get('links');
  $form['links'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Label'),
      $this
        ->t('Enabled'),
      $this
        ->t('Weight'),
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'bibcite-links-order-weight',
      ],
    ],
  ];
  foreach ($this->bibciteLinkManager
    ->getDefinitions() as $plugin_id => $definition) {
    $weight = !empty($links[$plugin_id]['weight']) ? (int) $links[$plugin_id]['weight'] : NULL;
    $form['links'][$plugin_id]['#attributes']['class'][] = 'draggable';
    $form['links'][$plugin_id]['#weight'] = $weight;
    $form['links'][$plugin_id]['label'] = [
      '#plain_text' => $definition['label'],
    ];
    $form['links'][$plugin_id]['enabled'] = [
      '#type' => 'checkbox',
      '#default_value' => isset($links[$plugin_id]['enabled']) ? $links[$plugin_id]['enabled'] : TRUE,
    ];
    $form['links'][$plugin_id]['weight'] = [
      '#type' => 'weight',
      '#title' => t('Weight for @title', [
        '@title' => $definition['label'],
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $weight,
      '#attributes' => [
        'class' => [
          'bibcite-links-order-weight',
        ],
      ],
    ];
  }
  uasort($form['links'], 'Drupal\\Component\\Utility\\SortArray::sortByWeightProperty');
  return parent::buildForm($form, $form_state);
}