public function GeneralUserReferenceFormatter::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/ GeneralUserReferenceFormatter.php, line 384
Class
- GeneralUserReferenceFormatter
- Formats a user entity reference as one or more links.
Namespace
Drupal\formatter_suite\Plugin\Field\FieldFormatterCode
public function settingsForm(array $form, FormStateInterface $formState) {
$this
->sanitizeSettings();
$isMultiple = $this->fieldDefinition
->getFieldStorageDefinition()
->isMultiple();
// 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['userReferenceStyle'] = [
'#title' => $this
->t('Link title'),
'#type' => 'select',
'#options' => self::getUserReferenceStyles(),
'#default_value' => $this
->getSetting('userReferenceStyle'),
'#weight' => $weight++,
'#wrapper_attributes' => [
'class' => [
'formatter_suite-general-user-reference-style',
],
],
'#attributes' => [
'class' => [
'userReferenceStyle-' . $marker,
],
],
];
$tokenUi = '';
if ($this->tokenTreeBuilder !== NULL) {
try {
$tokenUi = $this->tokenTreeBuilder
->buildRenderable([
'user',
], [
// Focus on the entity type's tokens only.
'global_types' => FALSE,
// 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.
}
}
$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-user-reference-title-custom-text',
],
],
'#states' => [
'visible' => [
'.userReferenceStyle-' . $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-user-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-user-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-user-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-user-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-user-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-user-reference-list-separator',
],
],
'#states' => [
'visible' => [
'.listStyle-' . $marker => [
'value' => 'span',
],
],
],
];
}
return $elements;
}