You are here

public function ViewsFieldFormatter::settingsForm in Views field formatter 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/ViewsFieldFormatter.php \Drupal\views_field_formatter\Plugin\Field\FieldFormatter\ViewsFieldFormatter::settingsForm()

Returns a form to configure settings for the formatter.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the formatter. The field_ui module takes care of handling submitted form values.

Parameters

array $form: The form where the settings form is being included in.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form elements for the formatter settings.

Overrides FormatterBase::settingsForm

File

src/Plugin/Field/FieldFormatter/ViewsFieldFormatter.php, line 95

Class

ViewsFieldFormatter
Class ViewsFieldFormatter.

Namespace

Drupal\views_field_formatter\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $element = parent::settingsForm($form, $form_state);
  $options = [];
  foreach (Views::getAllViews() as $view) {
    foreach ($view
      ->get('display') as $display) {
      $options[$view
        ->get('label')][$view
        ->get('id') . '::' . $display['id']] = \sprintf('%s - %s', $view
        ->get('label'), $display['display_title']);
    }
  }
  if ([] === $options) {
    $element['help'] = [
      '#markup' => '<p>' . $this
        ->t('No available Views were found.') . '</p>',
    ];
    return $element;
  }
  $element['view'] = [
    '#title' => $this
      ->t('View'),
    '#description' => $this
      ->t("Select the view that will be displayed instead of the field's value."),
    '#type' => 'select',
    '#default_value' => $this
      ->getSetting('view'),
    '#options' => $options,
  ];
  $element['arguments'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('View Arguments'),
      $this
        ->t('Weight'),
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'arguments-order-weight',
      ],
    ],
    '#caption' => $this
      ->t('Select the arguments to send to the views, you can reorder them.
                          These arguments can be used as contextual filters in the selected View.'),
  ];
  $default_arguments = \array_keys(\array_filter($this
    ->getSetting('arguments'), static function ($argument) {
    return $argument['checked'];
  }));
  $arguments = \array_combine($default_arguments, $default_arguments);
  foreach ($this
    ->getDefaultArguments() as $argument_id => $argument_name) {
    $arguments[$argument_id] = $argument_name;
  }
  foreach ($arguments as $argument_id => $argument_name) {
    $element['arguments'][$argument_id] = [
      'checked' => [
        '#type' => 'checkbox',
        '#title' => $argument_name,
        '#default_value' => \in_array($argument_id, $default_arguments, TRUE),
      ],
      'weight' => [
        '#type' => 'weight',
        '#title' => $this
          ->t('Weight for @title', [
          '@title' => $argument_name,
        ]),
        '#title_display' => 'invisible',
        '#attributes' => [
          'class' => [
            'arguments-order-weight',
          ],
        ],
      ],
      '#attributes' => [
        'class' => [
          'draggable',
        ],
      ],
    ];
  }
  $element['hide_empty'] = [
    '#title' => $this
      ->t('Hide empty views'),
    '#description' => $this
      ->t('Do not display the field if the view is empty.'),
    '#type' => 'checkbox',
    '#default_value' => (bool) $this
      ->getSetting('hide_empty'),
  ];
  $element['multiple'] = [
    '#title' => $this
      ->t('Multiple'),
    '#description' => $this
      ->t('If the field is configured as multiple (<em>greater than one</em>),
                          should we display a view per item ? If selected, there will be one view per item.'),
    '#type' => 'checkbox',
    '#default_value' => (bool) $this
      ->getSetting('multiple'),
  ];
  $element['implode_character'] = [
    '#title' => $this
      ->t('Implode with this character'),
    '#description' => $this
      ->t('If it is set, all field values are imploded with this character (<em>ex: a simple comma</em>)
                          and sent as one views argument. Empty to disable.'),
    '#type' => 'textfield',
    '#default_value' => $this
      ->getSetting('implode_character'),
    '#states' => [
      'visible' => [
        ':input[name="fields[' . $this->fieldDefinition
          ->getName() . '][settings_edit_form][settings][multiple]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  return $element;
}