You are here

public function JwplayerFormatter::settingsForm in JW Player 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/JwplayerFormatter.php, line 42

Class

JwplayerFormatter
Plugin implementation of the 'foo_formatter' formatter.

Namespace

Drupal\jw_player\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $presets = Jw_player::loadMultiple();
  $options = [];
  if (!empty($presets)) {
    foreach ($presets as $type => $type_info) {
      $options[$type] = $type_info
        ->label();
    }
    $element['jwplayer_preset'] = [
      '#title' => $this
        ->t('Select preset'),
      '#type' => 'select',
      '#empty_option' => $this
        ->t('- No preset selected -'),
      '#default_value' => $this
        ->getSetting('jwplayer_preset') ?: 'none',
      '#options' => $options,
    ];
    $element['links'] = [
      '#theme' => 'links',
      '#links' => [
        [
          'url' => Url::fromRoute('jw_player.preset_add'),
          'title' => $this
            ->t('Create new preset'),
        ],
        [
          'url' => Url::fromRoute('entity.jw_player.collection'),
          'title' => $this
            ->t('Manage presets'),
        ],
      ],
    ];
    if ($this
      ->getSetting('jwplayer_preset') && $this
      ->getSetting('jwplayer_preset') != 'none') {
      $element['links']['#links'][] = [
        'url' => Url::fromRoute('entity.jw_player.edit_form', [
          'jw_player' => $this
            ->getSetting('jwplayer_preset'),
        ]),
        'title' => $this
          ->t('Manage selected preset'),
      ];
    }

    // Add support for configurable preview images.
    if (\Drupal::moduleHandler()
      ->moduleExists('image') && $this->fieldDefinition
      ->getTargetEntityTypeId() && $this->fieldDefinition
      ->getTargetBundle()) {
      $options = [];
      $field_definitions = \Drupal::service('entity_field.manager')
        ->getFieldDefinitions($this->fieldDefinition
        ->getTargetEntityTypeId(), $this->fieldDefinition
        ->getTargetBundle());

      // @todo add support for fields on file references.
      foreach ($field_definitions as $field_name => $field_definition) {
        if ($field_definition
          ->getType() == 'image') {

          // Structure of the key can be used later on in the formatter's view
          // callback in order to fetch the image uri from the configure field.
          $options[$this->fieldDefinition
            ->getTargetEntityTypeId() . ':' . $this->fieldDefinition
            ->getTargetBundle() . '|' . $field_name] = $field_definition
            ->getLabel() . ' (' . $this->fieldDefinition
            ->getTargetEntityTypeId() . ':' . $this->fieldDefinition
            ->getTargetBundle() . ')';
        }
      }
      if ($options) {
        $element['preview_image_field'] = [
          '#title' => $this
            ->t('Preview image source'),
          '#description' => $this
            ->t('You can choose an image field directly on this node type, or on any entity of an entity/file/term reference field on this content type.'),
          '#type' => 'select',
          '#options' => $options,
          '#default_value' => $this
            ->getSetting('preview_image_field') ? $this
            ->getSetting('preview_image_field') : '',
          '#empty_option' => $this
            ->t('None'),
        ];
        $options = image_style_options();
        $element['preview_image_style'] = [
          '#title' => $this
            ->t('Preview image style'),
          '#description' => $this
            ->t('Choose an image style that will be used for the preview image.'),
          '#type' => 'select',
          '#options' => $options,
          '#default_value' => $this
            ->getSetting('preview_image_style') ? $this
            ->getSetting('preview_image_style') : '',
          '#states' => [
            'invisible' => [
              array(
                ':input[name="fields[' . $this->fieldDefinition
                  ->getName() . '][settings_edit_form][settings][preview_image_field]"]' => [
                  'value' => '',
                ],
              ),
            ],
          ],
        ];
      }
    }
  }
  else {
    $element['no_preset_message'] = [
      '#markup' => '<div class="messages warning">' . $this
        ->t('No presets are available. Please <a href="@create">create a preset</a> in order to proceed.', [
        '@create' => Url::fromRoute('jw_player.preset_add')
          ->toString(),
      ]) . '</div>',
    ];
  }
  return $element;
}