public function GeneralEntityReferenceFormatter::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/ GeneralEntityReferenceFormatter.php, line 558
Class
- GeneralEntityReferenceFormatter
- Formats an entity reference as one or more links.
Namespace
Drupal\formatter_suite\Plugin\Field\FieldFormatterCode
public function settingsForm(array $form, FormStateInterface $formState) {
$this
->sanitizeSettings();
$storageDef = $this->fieldDefinition
->getFieldStorageDefinition();
$isMultiple = $storageDef
->isMultiple();
// Get the token UI.
//
// The Drupal core token API can return a UI for selecting tokens and
// adding them an input field. This is returned by the token tree
// builder, optionally filtered by the token prefix.
//
// Showing all tokens in the system doesn't make sense here. All we
// want are the tokens relevant for the field's entity type. Normally,
// the token prefix is the entity type, but this is not enforced and
// the token API has no way to look up the prefix. So, we assume the
// prefix is the entity type, except for known Drupal core exceptions.
$entityType = $storageDef
->getSetting('target_type');
$fixPrefixes = self::getWellKnownTokenPrefixExceptions();
if (isset($fixPrefixes[$entityType]) === TRUE) {
$prefix = $fixPrefixes[$entityType];
}
else {
$prefix = $entityType;
}
$tokenUi = '';
$tokensAvailable = FALSE;
if ($this->tokenService !== NULL) {
// Get token info and confirm that the prefix determined above has tokens.
// If it doesn't, then we'll need to fall back to showing all tokens
// since we cannot determine the right prefix to use.
$tokenInfo = $this->tokenService
->getInfo();
if (isset($tokenInfo['tokens'][$prefix]) === FALSE) {
// Prefix is unknown. Don't use a prefix.
$prefix = '';
}
// Get the token UI.
if ($this->tokenTreeBuilder !== NULL) {
try {
$tokensAvailable = $prefix !== '';
$tokenUi = $this->tokenTreeBuilder
->buildRenderable($prefix === '' ? [] : [
$prefix,
], [
// Focus on the entity type's tokens only, if
// we found a usable prefix. Otherwise show everything in
// the hope that the user recognizes the tokens wanted.
'global_types' => !$tokensAvailable,
// Use the click-to-insert UI.
'click_insert' => TRUE,
// Don't show restricted info.
'show_restricted' => FALSE,
// Do show nested tokens, such as for customized dates.
'show_nested' => TRUE,
]);
} catch (\Exception $e) {
// Cannot create the token tree builder. Show nothing.
}
}
}
// Get entity reference styles.
//
// If the above checks found tokens, then use a larger list of
// available styles, some of which depend upon tokens. But if
// the above checks did not find tokens, then reduce the style
// list to those we can safely implement in code.
if ($tokensAvailable === TRUE) {
$styles = self::getEntityReferenceStyles();
}
else {
$styles = self::getMinimalEntityReferenceStyles();
}
// 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 = parent::settingsForm($form, $formState);
$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.
$elements['entityReferenceStyle'] = [
'#title' => $this
->t('Link title'),
'#type' => 'select',
'#options' => $styles,
'#default_value' => $this
->getSetting('entityReferenceStyle'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-entity-reference-style',
],
],
'#attributes' => [
'class' => [
'entityReferenceStyle-' . $marker,
],
],
];
$elements['titleCustomText'] = [
'#title' => $this
->t('Custom text'),
'#type' => 'textfield',
'#size' => 10,
'#default_value' => $this
->getSetting('titleCustomText'),
'#description' => $tokenUi,
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-entity-reference-title-custom-text',
],
],
'#states' => [
'visible' => [
'.entityReferenceStyle-' . $marker => [
'value' => '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-entity-reference-classes',
],
],
];
$elements['sectionBreak2'] = [
'#markup' => '<div class="formatter_suite-section-break"></div>',
'#weight' => $weight++,
];
$elements['showLink'] = [
'#title' => $this
->t('Link to the entity'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('showLink'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-entity-reference-show-link',
],
],
'#attributes' => [
'class' => [
'showLink-' . $marker,
],
],
];
$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-entity-reference-open-link-in',
],
],
'#states' => [
'visible' => [
'.showLink-' . $marker => [
'checked' => TRUE,
],
],
],
];
$elements['linkTopic'] = [
'#title' => $this
->t('Annotate link as'),
'#type' => 'select',
'#options' => self::getLinkTopicValues(),
'#default_value' => $this
->getSetting('linkTopic'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-entity-reference-link-topic',
],
],
'#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' => self::getListStyles(),
'#default_value' => $this
->getSetting('listStyle'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-entity-reference-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-entity-reference-list-separator',
],
],
'#states' => [
'visible' => [
'.listStyle-' . $marker => [
'value' => 'span',
],
],
],
];
}
return $elements;
}