You are here

public function Links::buildOptionsForm in Bibliography & Citation 8

Same name and namespace in other branches
  1. 2.0.x modules/bibcite_entity/src/Plugin/views/field/Links.php \Drupal\bibcite_entity\Plugin\views\field\Links::buildOptionsForm()

Default options form that provides the label widget that all fields should have.

Overrides FieldPluginBase::buildOptionsForm

File

modules/bibcite_entity/src/Plugin/views/field/Links.php, line 84

Class

Links
Field handler to render links for bibcite entity.

Namespace

Drupal\bibcite_entity\Plugin\views\field

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  $form['override_default'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Override default links settings'),
    '#default_value' => $this->options['override_default'],
  ];

  // @todo This is a copy of bibcite_entity_reference_settings_links form. Find a way to reuse form code.
  $form['links'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Links overrides'),
    '#states' => [
      'visible' => [
        ':input[name="options[override_default]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
    'overrides' => [
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Label'),
        $this
          ->t('Enabled'),
        $this
          ->t('Weight'),
      ],
      '#tabledrag' => [
        [
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'bibcite-links-order-weight',
        ],
      ],
    ],
  ];
  $overrides_table =& $form['links']['overrides'];
  $links = isset($this->options['links']['overrides']) ? $this->options['links']['overrides'] : [];
  foreach ($this->linkPluginManager
    ->getDefinitions() as $plugin_id => $definition) {
    $weight = !empty($links[$plugin_id]['weight']) ? (int) $links[$plugin_id]['weight'] : NULL;
    $overrides_table[$plugin_id]['#attributes']['class'][] = 'draggable';
    $overrides_table[$plugin_id]['#weight'] = $weight;
    $overrides_table[$plugin_id]['label'] = [
      '#plain_text' => $definition['label'],
    ];
    $overrides_table[$plugin_id]['enabled'] = [
      '#type' => 'checkbox',
      '#default_value' => isset($links[$plugin_id]['enabled']) ? $links[$plugin_id]['enabled'] : TRUE,
    ];
    $overrides_table[$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($overrides_table, 'Drupal\\Component\\Utility\\SortArray::sortByWeightProperty');
}