Repository URL to install this package:
|
Version:
2.5.1 ▾
|
<?php
namespace Drupal\dds_global_content;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\Renderer;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Utility\Token;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class GlobalContentPluginBase extends PluginBase implements GlobalContentPluginInterface, ContainerFactoryPluginInterface {
use StringTranslationTrait;
/** @var BubbleableMetadata */
protected $bubbleable_metadata;
protected $config;
protected $moduleHandler;
protected $token;
public function __construct(ModuleHandlerInterface $moduleHandler, Token $token, ImmutableConfig $config, array $configuration, $plugin_id, $plugin_definition) {
$this->config = $config;
$this->moduleHandler = $moduleHandler;
$this->token = $token;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* @return ImmutableConfig
*/
protected function getConfig() {
return $this->config;
}
/**
* @return bool
*/
protected function isTokenSupportEnabled() {
if ($this->moduleHandler
->moduleExists('token') && $this->getConfig()->get('tokens')) {
return TRUE;
}
else {
return FALSE;
}
}
protected function getFormStateKey(GlobalContent $globalContent) {
if (empty($globalContent->getSubgroupName())) {
$subgroup = '0';
}
else {
$subgroup = $globalContent->getSubgroupID();
}
return [$subgroup, $globalContent->getMachineName()];
}
/**
* @param $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*
* @return bool
*/
public function validate(&$form, FormStateInterface $form_state) {
return TRUE;
}
/**
* @param \Drupal\dds_global_content\GlobalContent $globalContent
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
public function submit(GlobalContent $globalContent, FormStateInterface $form_state) {
$globalContent->setData($form_state->getValue($this->getFormStateKey($globalContent)));
}
/**
* @param \Drupal\dds_global_content\ViewableGlobalContent $view
* @param $text
*
* @return string
*/
protected function prepareText(ViewableGlobalContent $view, $text) {
if ($this->isTokenSupportEnabled()) {
return $this->token
->replace($text, $view->getTokenData(), [], $this->bubbleable_metadata);
}
else {
return $text;
}
}
/**
* @param ViewableGlobalContent $view
*
* @return array
* A render array to render the global content type.
*/
public function render(ViewableGlobalContent $view) {
$this->bubbleable_metadata = new BubbleableMetadata();
$build = [];
$this->renderBuild($view, $build);
$this->bubbleable_metadata->applyTo($build);
return $build;
}
/**
* @param \Drupal\dds_global_content\ViewableGlobalContent $view
* @param array $build
*/
abstract function renderBuild(ViewableGlobalContent $view, array &$build);
/**
* @param \Drupal\dds_global_content\ViewableGlobalContent $view
* @param string $key
*
* @return mixed
*/
public function value(ViewableGlobalContent $view, $key = '') {
/** @var Renderer $renderer */
static $renderer = NULL;
if (is_null($renderer)) {
$renderer = \Drupal::service('renderer');
}
$build = $this->render($view);
return $renderer->renderPlain($build);
}
/**
* @param $setting
*
* @return mixed|null
*/
public function getRenderSettings($setting) {
$definitions = $this->getPluginDefinition();
if (!empty($definitions['render_settings'][$setting])) {
return $definitions['render_settings'][$setting];
}
return NULL;
}
/**
* @param \Drupal\Core\Config\Config $config
* @param \Drupal\Core\Form\FormStateInterface $form_state
*
* @return array
*/
public function settingsForm(Config $config, FormStateInterface $form_state) {
return [];
}
/**
* @param \Drupal\Core\Config\Config $config
* @param \Drupal\Core\Form\FormStateInterface $form_state
*
* @return bool
*/
public function settingsValidate(Config $config, FormStateInterface $form_state) {
return TRUE;
}
/**
* @param \Drupal\Core\Config\Config $config
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
public function settingsSubmit(Config $config, FormStateInterface $form_state) {
}
static public function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$configFactory = $container->get('config.factory');
$config = $configFactory->get('dds_global_content.settings');
$moduleHandler = $container->get('module_handler');
$token = $container->get('token');
return new static($moduleHandler, $token, $config, $configuration, $plugin_id, $plugin_definition);
}
}