You are here

public function FileDownloadLink::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

src/Plugin/Field/FieldFormatter/FileDownloadLink.php, line 125

Class

FileDownloadLink
Plugin implementation of the 'file_download_link' formatter.

Namespace

Drupal\file_download_link\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $form = parent::settingsForm($form, $form_state);
  if ($this->fieldDefinition
    ->getTargetEntityTypeId() == 'media') {
    if (!$this->moduleHandler
      ->moduleExists('file_download_link_media')) {
      $form['media_warning'] = [
        '#type' => 'container',
        '#markup' => $this
          ->t("Did you know the file_download_link_media module allows you render a Media reference field as a link to the Media's source file or image? Consider enabling the module if that sounds helpful."),
        '#attributes' => [
          'class' => [
            'messages messages--warning',
          ],
        ],
      ];
    }
  }
  $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 file. If left empty, the filename will be used.'),
  ];
  if ($this->moduleHandler
    ->moduleExists('token')) {
    $form['tokens'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        $this->tokenEntityMapper
          ->getTokenTypeForEntityType('file'),
        $this->tokenEntityMapper
          ->getTokenTypeForEntityType($this->fieldDefinition
          ->getTargetEntityTypeId()),
      ],
    ];
    $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($this->fieldDefinition
          ->getTargetEntityTypeId()),
      ],
    ];
  }
  return $form;
}