public function GeneralLinkFormatter::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 FormatterBase::settingsForm
File
- src/
Plugin/ Field/ FieldFormatter/ GeneralLinkFormatter.php, line 251
Class
- GeneralLinkFormatter
- Formats a link field as one or more links.
Namespace
Drupal\formatter_suite\Plugin\Field\FieldFormatterCode
public function settingsForm(array $form, FormStateInterface $formState) {
$this
->sanitizeSettings();
//
// Get field settings.
//
// - If the link's URL is required to be internal, then below we can omit
// form items that are only useful for external links.
//
// - If the link has no title, then we can adjust form items to omit using
// the link field's title.
$fieldSettings = $this
->getFieldSettings();
$isMultiple = $this->fieldDefinition
->getFieldStorageDefinition()
->isMultiple();
$linkIsInternalOnly = FALSE;
if ($fieldSettings['link_type'] === LinkItemInterface::LINK_INTERNAL) {
$linkIsInternalOnly = TRUE;
}
$linkTitleDisabled = FALSE;
if ($fieldSettings['title'] === DRUPAL_DISABLED) {
$linkTitleDisabled = TRUE;
}
// 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();
// Add branding.
$elements = [];
$elements = Branding::addFieldFormatterBranding($elements);
$elements['#attached']['library'][] = 'formatter_suite/formatter_suite.fieldformatter';
// Add description.
//
// Use a large negative weight to insure it comes first.
$elements['description'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $this
->getDescription(),
'#weight' => -1000,
'#attributes' => [
'class' => [
'formatter_suite-settings-description',
],
],
];
$weight = 0;
// Prompt for each setting.
if ($linkTitleDisabled === TRUE) {
$titleStyles = $this
->getTitleStylesFieldNoTitle();
}
else {
$titleStyles = $this
->getTitleStyles();
}
$elements['titleStyle'] = [
'#title' => $this
->t('Link title'),
'#type' => 'select',
'#options' => $titleStyles,
'#default_value' => $this
->getSetting('titleStyle'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-link-title-style',
],
],
'#attributes' => [
'class' => [
'titleStyle-' . $marker,
],
],
];
$elements['titleCustomText'] = [
'#title' => $this
->t('Custom text'),
'#type' => 'textfield',
'#size' => 10,
'#default_value' => $this
->getSetting('titleCustomText'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-link-title-custom-text',
],
],
'#states' => [
'visible' => [
'.titleStyle-' . $marker => [
'value' => 'text_custom',
],
],
],
];
$elements['sectionBreak1'] = [
'#markup' => '<div class="formatter_suite-section-break"></div>',
'#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-link-classes',
],
],
];
$elements['sectionBreak2'] = [
'#markup' => '<div class="formatter_suite-section-break"></div>',
'#weight' => $weight++,
];
$elements['showLink'] = [
'#title' => $this
->t('Link to the item'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('showLink'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-link-show-link',
],
],
'#attributes' => [
'class' => [
'showLink-' . $marker,
],
],
];
$elements['openLinkIn'] = [
'#title' => $this
->t('Use link to'),
'#type' => 'select',
'#options' => $this
->getOpenLinkInValues(),
'#default_value' => $this
->getSetting('openLinkIn'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-link-open-link-in',
],
],
'#states' => [
'visible' => [
'.showLink-' . $marker => [
'checked' => TRUE,
],
],
],
];
$elements['linkTopic'] = [
'#title' => $this
->t('Annotate link as'),
'#type' => 'select',
'#options' => $this
->getLinkTopicValues(),
'#default_value' => $this
->getSetting('linkTopic'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-link-link-topic',
],
],
'#states' => [
'visible' => [
'.showLink-' . $marker => [
'checked' => TRUE,
],
],
],
];
if ($linkIsInternalOnly === FALSE) {
$elements['noreferrer'] = [
'#title' => $this
->t('Do not pass the current site as the referrer ("noreferrer")'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('noreferrer'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-link-noreferrer',
],
],
'#states' => [
'visible' => [
'.showLink-' . $marker => [
'checked' => TRUE,
],
],
],
];
$elements['noopener'] = [
'#title' => $this
->t('Do not share the current page context ("noopener")'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('noopener'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-link-noopener',
],
],
'#states' => [
'visible' => [
'.showLink-' . $marker => [
'checked' => TRUE,
],
],
],
];
}
if ($linkIsInternalOnly === FALSE) {
$elements['nofollow'] = [
'#title' => $this
->t('Do not treat the link as an endorsement ("nofollow")'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('nofollow'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-link-nofollow',
],
],
'#states' => [
'visible' => [
'.showLink-' . $marker => [
'checked' => TRUE,
],
],
],
];
}
if ($isMultiple === TRUE) {
$elements['sectionBreak3'] = [
'#markup' => '<div class="formatter_suite-section-break"></div>',
'#weight' => $weight++,
];
$elements['listStyle'] = [
'#title' => $this
->t('List style'),
'#type' => 'select',
'#options' => $this
->getListStyles(),
'#default_value' => $this
->getSetting('listStyle'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-link-list-style',
],
],
'#attributes' => [
'class' => [
'listStyle-' . $marker,
],
],
];
$elements['listSeparator'] = [
'#title' => $this
->t('Separator'),
'#type' => 'textfield',
'#size' => 10,
'#default_value' => $this
->getSetting('listSeparator'),
'#weight' => $weight++,
'#attributes' => [
'autocomplete' => 'off',
'autocapitalize' => 'none',
'spellcheck' => 'false',
'autocorrect' => 'off',
],
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-link-list-separator',
],
],
'#states' => [
'visible' => [
'.listStyle-' . $marker => [
'value' => 'span',
],
],
],
];
}
return $elements;
}