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;
class SocialLinkBaseFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
protected $platforms;
protected $configFactory;
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;
}
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'));
}
public static function defaultSettings() {
return [
'new_tab' => TRUE,
] + parent::defaultSettings();
}
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;
}
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;
}
}