You are here

protected function ImagePopupFieldFormatter::getAdditionalImageFields in Simple Image Popup 2.x

Creates alt and title fields for a base image context field.

Parameters

string $base_field_id: The field to create more fields for.

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

Return value

array The fields.

1 call to ImagePopupFieldFormatter::getAdditionalImageFields()
ImagePopupFieldFormatter::settingsForm in src/Plugin/Field/FieldFormatter/ImagePopupFieldFormatter.php
Returns a form to configure settings for the formatter.

File

src/Plugin/Field/FieldFormatter/ImagePopupFieldFormatter.php, line 391

Class

ImagePopupFieldFormatter
Plugin implementation of the 'image_popup_field_formatter' formatter.

Namespace

Drupal\image_popup\Plugin\Field\FieldFormatter

Code

protected function getAdditionalImageFields($base_field_id, FormStateInterface $form_state) {
  $fields = [];

  // Create fields for alt and title text.
  foreach ([
    'title' => $this
      ->t('Title'),
    'alt' => $this
      ->t('alt'),
  ] as $id => $label) {

    // Create a field to select the method for adding alt or title text.
    $fields["{$base_field_id}_{$id}_source"] = [
      '#title' => $this
        ->t('@label source', [
        '@label' => $label,
      ]),
      '#type' => 'select',
      '#default_value' => $this
        ->getSetting("{$base_field_id}_{$id}_source"),
      '#options' => [
        'static' => $this
          ->t('Fixed value'),
      ],
      '#attributes' => [
        'data-states-selector' => "{$base_field_id}_{$id}_source",
      ],
    ];

    // Create a field for a static text value.
    $fields["{$base_field_id}_{$id}_static"] = [
      '#title' => $this
        ->t('@label text', [
        '@label' => $label,
      ]),
      '#type' => 'textfield',
      '#default_value' => $this
        ->getSetting("{$base_field_id}_{$id}_static"),
      '#states' => [
        'visible' => [
          ":input[data-states-selector=\"{$base_field_id}_{$id}_source\"]" => [
            'value' => 'static',
          ],
        ],
      ],
    ];

    // Check to see if there are other fields that can be used as alt or title
    // fields.
    $field_options = $this
      ->getFieldOptions($form_state);
    if ($field_options) {

      // Add the option to use a field value for the text.
      $fields["{$base_field_id}_{$id}_source"]['#options']['field'] = $this
        ->t('Field value');

      // Create a field for selecting a different field on the entity.
      $fields["{$base_field_id}_{$id}_field"] = [
        '#title' => $this
          ->t('@label text field', [
          '@label' => $label,
        ]),
        '#type' => 'select',
        '#default_value' => $this
          ->getSetting("{$base_field_id}_{$id}_field"),
        '#options' => $field_options,
        '#states' => [
          'visible' => [
            ":input[data-states-selector=\"{$base_field_id}_{$id}_source\"]" => [
              'value' => 'field',
            ],
          ],
        ],
      ];
    }
  }
  return $fields;
}