You are here

SocialLinkBaseFormatter.php in Social Link Field 8

File

src/Plugin/Field/FieldFormatter/SocialLinkBaseFormatter.php
View source
<?php

namespace Drupal\social_link_field\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Base class for 'Field formatter' plugin implementations.
 */
class SocialLinkBaseFormatter extends FormatterBase implements ContainerFactoryPluginInterface {

  /**
   * Platform of social networks.
   *
   * @var array
   */
  protected $platforms;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * {@inheritdoc}
   */
  public function __construct($plugin_id, $plugin_definition, $field_definition, array $settings, $label, $view_mode, array $third_party_settings, $platforms_service, $config_factory) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
    $this->platforms = $platforms_service
      ->getPlatforms();
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
      ->get('plugin.manager.social_link_field.platform'), $container
      ->get('config.factory'));
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'new_tab' => TRUE,
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element['new_tab'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Open link in new tab'),
      '#default_value' => $this
        ->getSetting('new_tab'),
    ];
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $entity = $items
      ->getParent()
      ->getValue();
    $element = [
      '#theme' => 'social_link_field_formatter',
      '#new_tab' => $this
        ->getSetting('new_tab'),
      '#attributes' => [
        'data-quickedit-field-id' => implode('/', [
          $entity
            ->getEntityTypeId(),
          $entity
            ->id(),
          $items
            ->getName(),
          $langcode,
          $this->viewMode,
        ]),
      ],
    ];
    return $element;
  }

}

Classes

Namesort descending Description
SocialLinkBaseFormatter Base class for 'Field formatter' plugin implementations.