You are here

public function FileDownloadLinkMedia::settingsForm in File Download Link 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

modules/file_downoad_link_media/src/Plugin/Field/FieldFormatter/FileDownloadLinkMedia.php, line 111

Class

FileDownloadLinkMedia
Plugin implementation of the 'file_download_link_media' formatter.

Namespace

Drupal\file_download_link_media\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $form = parent::settingsForm($form, $form_state);
  $form['link_text'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Link Text'),
    '#default_value' => $this
      ->getSetting('link_text'),
    '#description' => $this
      ->t('This text is linked to the media source file. If left empty, the filename will be used.'),
  ];
  if ($this->moduleHandler
    ->moduleExists('token')) {
    $form['default']['tokens'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        $this->tokenEntityMapper
          ->getTokenTypeForEntityType('file'),
        $this->tokenEntityMapper
          ->getTokenTypeForEntityType('media'),
      ],
    ];
    $form['token_example'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Example Token'),
      '0' => [
        '#markup' => $this
          ->getTokenExampleMarkup(),
      ],
    ];
  }
  else {
    $form['token_warning'] = [
      '#type' => 'container',
      '#markup' => $this
        ->getTokenWarningMarkup(),
      '#attributes' => [
        'class' => [
          'messages messages--warning',
        ],
      ],
    ];
  }
  $form['new_tab'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Open file in new tab'),
    '#default_value' => $this
      ->getSetting('new_tab'),
  ];
  $form['force_download'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Force Download'),
    '#default_value' => $this
      ->getSetting('force_download'),
    '#description' => $this
      ->t('This adds the <i>download</i> attribute to the link, which works in many modern browsers.'),
  ];
  $form['link_title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Link Title'),
    '#default_value' => $this
      ->getSetting('link_title'),
    '#description' => $this
      ->t('Many browsers show the title attribute in a tooltip.'),
  ];
  if ($this->moduleHandler
    ->moduleExists('token')) {
    $form['tokens_2'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        $this->tokenEntityMapper
          ->getTokenTypeForEntityType('file'),
        $this->tokenEntityMapper
          ->getTokenTypeForEntityType($this->fieldDefinition
          ->getTargetEntityTypeId()),
      ],
    ];
  }
  $form['custom_classes'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Custom CSS Classes'),
    '#default_value' => $this
      ->getSetting('custom_classes'),
    '#description' => $this
      ->t('Enter space-separated CSS classes to be added to the link.'),
  ];
  if ($this->moduleHandler
    ->moduleExists('token')) {
    $form['tokens_3'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        $this->tokenEntityMapper
          ->getTokenTypeForEntityType('file'),
        $this->tokenEntityMapper
          ->getTokenTypeForEntityType('media'),
      ],
    ];
  }
  return $form;
}