public function GeneralImageFormatter::settingsForm in Formatter Suite 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 ImageFormatter::settingsForm
File
- src/
Plugin/ Field/ FieldFormatter/ GeneralImageFormatter.php, line 344
Class
- GeneralImageFormatter
- Formats an image.
Namespace
Drupal\formatter_suite\Plugin\Field\FieldFormatterCode
public function settingsForm(array $form, FormStateInterface $formState) {
// Below, some checkboxes and select choices show/hide other form
// elements. We use Drupal's obscure 'states' feature, which adds
// Javascript to elements to auto show/hide based upon a set of
// simple conditions.
//
// Those conditions need to reference the form elements to check
// (e.g. a checkbox), but the element ID and name are automatically
// generated by the parent form. We cannot set them, or predict them,
// so we cannot use them. We could use a class, but this form may be
// shown multiple times on the same page, so a simple class would not be
// unique. Instead, we create classes for this form only by adding a
// random number marker to the end of the class name.
$marker = rand();
// Do not start with the parent form, which is ugly. Add that form's
// elements directly below.
$elements = [];
// Add branding.
$elements = Branding::addFieldFormatterBranding($elements);
$elements['#attached']['library'][] = 'formatter_suite/formatter_suite.fieldformatter';
// Add description.
$elements['description'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $this
->getDescription(),
'#weight' => -1000,
'#attributes' => [
'class' => [
'formatter_suite-settings-description',
],
],
];
$weight = 100;
// Determine if the field has a title.
$fieldDefinition = $this->fieldDefinition;
$fieldSettings = $fieldDefinition
->getSettings();
$hasTitle = FALSE;
if (isset($fieldSettings['title_field']) === TRUE && boolval($fieldSettings['title_field']) === TRUE) {
$hasTitle = TRUE;
}
// Get image styles and a link to the styles configuration page.
$imageStyles = image_style_options(FALSE);
$imageStylesPage = Link::fromTextAndUrl($this
->t('Configure image styles'), Url::fromRoute('entity.image_style.collection'));
$imageStyleAllowed = $this->currentUser
->hasPermission('administer image styles');
// Prompt for each setting.
$elements['image_style'] = [
'#title' => $this
->t('Image style'),
'#type' => 'select',
'#options' => $imageStyles,
'#empty_option' => $this
->t('None (original image)'),
'#default_value' => $this
->getSetting('image_style'),
'#description' => $imageStylesPage
->toRenderable() + [
'#access' => $imageStyleAllowed,
],
'#weight' => $weight++,
];
$elements['classes'] = [
'#title' => $this
->t('Custom classes'),
'#type' => 'textfield',
'#size' => 10,
'#default_value' => $this
->getSetting('classes'),
'#weight' => $weight++,
'#attributes' => [
'autocomplete' => 'off',
'autocapitalize' => 'none',
'spellcheck' => 'false',
'autocorrect' => 'off',
],
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-image-classes',
],
],
];
$elements['sectionBreak2'] = [
'#markup' => '<div class="formatter_suite-section-break"></div>',
'#weight' => $weight++,
];
$elements['image_link'] = [
'#title' => $this
->t('Link image'),
'#type' => 'select',
'#options' => self::getLinkTypes(),
'#empty_option' => $this
->t('Do not link the image'),
'#default_value' => $this
->getSetting('image_link'),
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-image-link-type',
],
],
'#attributes' => [
'class' => [
'image_link-' . $marker,
],
],
'#weight' => $weight++,
];
$elements['openLinkIn'] = [
'#title' => $this
->t('Use link to'),
'#type' => 'select',
'#options' => self::getOpenLinkInValues(),
'#default_value' => $this
->getSetting('openLinkIn'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-image-open-link-in',
],
],
'#states' => [
'invisible' => [
'.image_link-' . $marker => [
'value' => '',
],
],
],
];
$elements['sectionBreak3'] = [
'#markup' => '<div class="formatter_suite-section-break"></div>',
'#weight' => $weight++,
];
$elements['captionLocation'] = [
'#title' => $this
->t('Caption location'),
'#type' => 'select',
'#options' => self::getCaptionLocations(),
'#default_value' => $this
->getSetting('captionLocation'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-image-caption-location',
],
],
'#attributes' => [
'class' => [
'captionLocation-' . $marker,
],
],
];
if ($hasTitle === TRUE) {
$elements['captionIncludeTitle'] = [
'#title' => $this
->t('Include title in caption'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('captionIncludeTitle'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-image-caption-include-title',
],
],
'#states' => [
'invisible' => [
'.captionLocation-' . $marker => [
'value' => 'none',
],
],
],
];
}
$elements['captionIncludeFilename'] = [
'#title' => $this
->t('Include file name in caption'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('captionIncludeFilename'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-image-caption-include-filename',
],
],
'#states' => [
'invisible' => [
'.captionLocation-' . $marker => [
'value' => 'none',
],
],
],
];
$elements['captionIncludeSize'] = [
'#title' => $this
->t('Include file size in caption'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('captionIncludeSize'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-image-caption-include-size',
],
],
'#states' => [
'invisible' => [
'.captionLocation-' . $marker => [
'value' => 'none',
],
],
],
];
$elements['captionIncludeDimensions'] = [
'#title' => $this
->t('Include image dimensions in caption'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('captionIncludeDimensions'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-image-caption-include-dimensions',
],
],
'#states' => [
'invisible' => [
'.captionLocation-' . $marker => [
'value' => 'none',
],
],
],
];
$elements['captionIncludeMime'] = [
'#title' => $this
->t('Include MIME type in caption'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('captionIncludeMime'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-image-caption-include-mime',
],
],
'#states' => [
'invisible' => [
'.captionLocation-' . $marker => [
'value' => 'none',
],
],
],
];
return $elements;
}