View source
<?php
namespace Drupal\manage_display\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\BasicStringFormatter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
class TitleFormatter extends BasicStringFormatter {
public function viewElements(FieldItemListInterface $items, $langcode = NULL) {
$output = [];
$parent = $items
->getParent()
->getValue();
foreach ($items as $item) {
$text = $item
->getValue()['value'];
if ($this
->getSetting('linked') && $this
->canLink()) {
$url = $parent
->isNew() ? Url::fromRoute('<front>') : $parent
->toUrl();
$text = Link::fromTextAndUrl($text, $url)
->toString();
}
$output[] = [
'#type' => 'html_tag',
'#tag' => $this
->getSetting('tag'),
'#value' => $text,
];
}
return $output;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$heading_options = [
'span' => 'span',
'div' => 'div',
];
foreach (range(1, 5) as $level) {
$heading_options['h' . $level] = 'H' . $level;
}
$form['tag'] = [
'#title' => $this
->t('Tag'),
'#type' => 'select',
'#description' => $this
->t('Select the tag which will be wrapped around the title.'),
'#options' => $heading_options,
'#default_value' => $this
->getSetting('tag'),
];
$form['linked'] = [
'#title' => $this
->t('Link to the Content'),
'#type' => 'checkbox',
'#description' => $this
->t('Wrap the title with a link to the content.'),
'#default_value' => $this
->getSetting('linked'),
'#access' => $this
->canLink(),
];
return $form;
}
public static function defaultSettings() {
return [
'tag' => 'h2',
'linked' => TRUE,
];
}
public function settingsSummary() {
$settings = $this
->getSettings();
$replacements = [
'@tag' => $settings['tag'],
];
if ($settings['linked'] && $this
->canLink()) {
$summary[] = $this
->t('Display as @tag, linked to content', $replacements);
}
else {
$summary[] = $this
->t('Display as @tag', $replacements);
}
return $summary;
}
public function canLink() {
$entity_type = \Drupal::entityTypeManager()
->getDefinition($this->fieldDefinition
->getTargetEntityTypeId());
return $entity_type
->hasLinkTemplate('canonical');
}
}