gatsby_endpoints-8.x-1.0-alpha1/src/Plugin/GatsbyEndpointBase.php
src/Plugin/GatsbyEndpointBase.php
<?php
namespace Drupal\gatsby_endpoints\Plugin;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Plugin\PluginWithFormsInterface;
use Drupal\Core\Plugin\PluginWithFormsTrait;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Base class for Gatsby endpoint plugins.
*/
abstract class GatsbyEndpointBase extends PluginBase implements GatsbyEndpointInterface, PluginWithFormsInterface {
use PluginWithFormsTrait;
use StringTranslationTrait;
/**
* The transliteration service.
*
* @var \Drupal\Component\Transliteration\TransliterationInterface
*/
protected $transliteration;
/**
* {@inheritdoc}
*/
public function label() {
if (!empty($this->configuration['label'])) {
return $this->configuration['label'];
}
}
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->setConfiguration($configuration);
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep(
$this->baseConfigurationDefaults(),
$this->defaultConfiguration(),
$configuration
);
}
/**
* Returns generic default configuration for Gatsby endpoint plugins.
*
* @return array
* An associative array with the default configuration.
*/
protected function baseConfigurationDefaults() {
return [
'id' => $this->getPluginId(),
'label' => '',
'provider' => $this->pluginDefinition['provider'],
];
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [];
}
/**
* {@inheritdoc}
*/
public function setConfigurationValue($key, $value) {
$this->configuration[$key] = $value;
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
$dependencies = [];
$definition = $this->getPluginDefinition();
$dependencies['module'][] = $definition['provider'];
return $dependencies;
}
/**
* {@inheritdoc}
*/
public function access(AccountInterface $account, $return_as_object = FALSE) {
$access = $this->gatsbyEndpointAccess($account);
return $return_as_object ? $access : $access->isAllowed();
}
/**
* Indicates whether the Gatsby endpoint should be shown.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The user session for which to check access.
*
* @return \Drupal\Core\Access\AccessResult
* The access result.
*
* @see self::access()
*/
protected function gatsbyEndpointAccess(AccountInterface $account) {
// By default, the endpoint is visible.
return AccessResult::allowed();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$definition = $this->getPluginDefinition();
$form['provider'] = [
'#type' => 'value',
'#value' => $definition['provider'],
];
// Add plugin-specific settings for this Gatsby endpoint type.
$form += $this->gatsbyEndpointForm($form, $form_state);
return $form;
}
/**
* {@inheritdoc}
*/
public function gatsbyEndpointForm($form, FormStateInterface $form_state) {
return [];
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->gatsbyEndpointFormValidate($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function gatsbyEndpointFormValidate($form, FormStateInterface $form_state) {}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
if (!$form_state->getErrors()) {
$this->configuration['label'] = $form_state->getValue('label');
$this->configuration['provider'] = $form_state->getValue('provider');
$this->gatsbyEndpointSubmit($form, $form_state);
}
}
/**
* {@inheritdoc}
*/
public function gatsbyEndpointSubmit($form, FormStateInterface $form_state) {}
/**
* {@inheritdoc}
*/
public function getMachineNameSuggestion() {
$definition = $this->getPluginDefinition();
$admin_label = $definition['admin_label'];
$transliterated = $this->transliteration()
->transliterate($admin_label, LanguageInterface::LANGCODE_DEFAULT, '_');
$transliterated = mb_strtolower($transliterated);
$transliterated = preg_replace('@[^a-z0-9_.]+@', '', $transliterated);
return $transliterated;
}
/**
* Wraps the transliteration service.
*
* @return \Drupal\Component\Transliteration\TransliterationInterface
* The transliteration service.
*/
protected function transliteration() {
if (!$this->transliteration) {
$this->transliteration = \Drupal::transliteration();
}
return $this->transliteration;
}
/**
* Sets the transliteration service.
*
* @param \Drupal\Component\Transliteration\TransliterationInterface $transliteration
* The transliteration service.
*/
public function setTransliteration(TransliterationInterface $transliteration) {
$this->transliteration = $transliteration;
}
}
