You are here

public function FieldCollectionTabsFormatter::settingsForm in Field Collection Tab formatter 8

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/FieldCollectionTabsFormatter.php, line 85

Class

FieldCollectionTabsFormatter
Plugin implementation of the 'tabs' formatter.

Namespace

Drupal\field_collection_tabs\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $elements = array();
  $options = array(
    $this
      ->t('No titles'),
  );
  $field_definition = $this->fieldDefinition;
  $fields = $this->entityFieldManager
    ->getFieldDefinitions('field_collection_item', $field_definition
    ->getName());
  foreach ($fields as $field_name => $field) {
    if ($field
      ->getFieldStorageDefinition()
      ->isBaseField() == FALSE && $field
      ->getType() == 'string') {
      $options[$field_name] = $this
        ->t($field
        ->getLabel());
    }
  }
  $elements['title_field'] = array(
    '#type' => 'select',
    '#title' => 'Field to use for tab titles',
    '#description' => t('Select the field to use for tab titles'),
    '#default_value' => $this
      ->getSetting('title_field'),
    '#options' => $options,
  );
  $displays = $this->entityDisplayRepository
    ->getAllViewModes();
  if (isset($displays['field_collection_item']) && !empty($displays['field_collection_item'])) {
    $displays = $displays['field_collection_item'];
    $options = array(
      $this
        ->t('Default'),
    );
    foreach ($displays as $view_mode => $info) {
      $options[$view_mode] = $info['label'];
    }
    $elements['view_mode'] = array(
      '#type' => 'select',
      '#title' => t('View mode'),
      '#options' => $options,
      '#default_value' => $this
        ->getSetting('view_mode'),
      '#description' => t('Select the view mode'),
    );
  }
  else {
    $elements['view_mode'] = array(
      '#markup' => $this
        ->t('No custom view modes for Field Collection Items'),
    );
  }
  return $elements;
}