You are here

public function ImageFieldFormatter::buildConfigurationForm in Entity Embed 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides FileFieldFormatter::buildConfigurationForm

File

src/Plugin/entity_embed/EntityEmbedDisplay/ImageFieldFormatter.php, line 150

Class

ImageFieldFormatter
Entity Embed Display reusing image field formatters.

Namespace

Drupal\entity_embed\Plugin\entity_embed\EntityEmbedDisplay

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);

  // File field support descriptions, but images do not.
  unset($form['description']);

  // Ensure that the 'Link image to: Content' setting is not available.
  if ($this
    ->getDerivativeId() == 'image') {
    unset($form['image_link']['#options']['content']);
  }
  $entity_element = $form_state
    ->get('entity_element');

  // The alt attribute is *required*, but we allow users to opt-in to empty
  // alt attributes for the very rare edge cases where that is valid by
  // specifying two double quotes as the alternative text in the dialog.
  // However, that *is* stored as an empty alt attribute, so if we're editing
  // an existing image (which means the src attribute is set) and its alt
  // attribute is empty, then we show that as two double quotes in the dialog.
  // @see https://www.drupal.org/node/2307647
  // Alt attribute behavior is taken from the Core image dialog to ensure a
  // consistent UX across various forms.
  // @see Drupal\editor\Form\EditorImageDialog::buildForm()
  $alt = $this
    ->getAttributeValue('alt', '');
  if ($alt === '') {

    // Do not change empty alt text to two double quotes if the previously
    // used Entity Embed Display plugin was not 'image:image'. That means that
    // some other plugin was used so if this image formatter is selected at a
    // later stage, then this should be treated as a new edit. We show two
    // double quotes in place of empty alt text only if that was filled
    // intentionally by the user.
    if (!empty($entity_element) && $entity_element['data-entity-embed-display'] == 'image:image') {
      $alt = MediaImageDecorator::EMPTY_STRING;
    }
  }

  // Add support for editing the alternate and title text attributes.
  $form['alt'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Alternate text'),
    '#default_value' => $alt,
    '#description' => $this
      ->t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'),
    '#parents' => [
      'attributes',
      'alt',
    ],
    '#required' => TRUE,
    '#required_error' => $this
      ->t('Alternative text is required.<br />(Only in rare cases should this be left empty. To create empty alternative text, enter <code>""</code> — two double quotes without any content).'),
    '#maxlength' => 512,
  ];
  $form['title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Title'),
    '#default_value' => $this
      ->getAttributeValue('title', ''),
    '#description' => t('The title is used as a tool tip when the user hovers the mouse over the image.'),
    '#parents' => [
      'attributes',
      'title',
    ],
    '#maxlength' => 1024,
  ];
  return $form;
}