3 namespace Drupal\aggregator\Plugin\Field\FieldFormatter;
5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Field\FormatterBase;
8 use Drupal\Core\Form\FormStateInterface;
12 * Plugin implementation of the 'aggregator_title' formatter.
15 * id = "aggregator_title",
16 * label = @Translation("Aggregator title"),
17 * description = @Translation("Formats an aggregator item or feed title with an optional link."),
23 class AggregatorTitleFormatter extends FormatterBase {
28 public static function defaultSettings() {
29 $options = parent::defaultSettings();
31 $options['display_as_link'] = TRUE;
38 public function settingsForm(array $form, FormStateInterface $form_state) {
39 $form = parent::settingsForm($form, $form_state);
41 $form['display_as_link'] = [
42 '#type' => 'checkbox',
43 '#title' => $this->t('Link to URL'),
44 '#default_value' => $this->getSetting('display_as_link'),
53 public function viewElements(FieldItemListInterface $items, $langcode) {
56 if ($items->getEntity()->getEntityTypeId() == 'aggregator_feed') {
57 $url_string = $items->getEntity()->getUrl();
60 $url_string = $items->getEntity()->getLink();
63 foreach ($items as $delta => $item) {
64 if ($this->getSetting('display_as_link') && $url_string) {
67 '#title' => $item->value,
68 '#url' => Url::fromUri($url_string),
72 $elements[$delta] = ['#markup' => $item->value];
82 public static function isApplicable(FieldDefinitionInterface $field_definition) {
83 return (($field_definition->getTargetEntityTypeId() === 'aggregator_item' || $field_definition->getTargetEntityTypeId() === 'aggregator_feed') && $field_definition->getName() === 'title');