View source
<?php
namespace Drupal\build_hooks\Plugin;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginWithFormsInterface;
use Drupal\Core\Plugin\PluginWithFormsTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Psr\Http\Message\ResponseInterface;
abstract class FrontendEnvironmentBase extends PluginBase implements FrontendEnvironmentInterface, PluginWithFormsInterface {
use PluginWithFormsTrait;
use StringTranslationTrait;
protected $transliteration;
public function label() {
if (!empty($this->configuration['label'])) {
return $this->configuration['label'];
}
}
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this
->setConfiguration($configuration);
}
public function getConfiguration() {
return $this->configuration;
}
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep($this
->baseConfigurationDefaults(), $this
->defaultConfiguration(), $configuration);
}
protected function baseConfigurationDefaults() {
return [
'id' => $this
->getPluginId(),
'label' => '',
'provider' => $this->pluginDefinition['provider'],
];
}
public function defaultConfiguration() {
return [];
}
public function setConfigurationValue($key, $value) {
$this->configuration[$key] = $value;
}
public function calculateDependencies() {
$dependencies = [];
$definition = $this
->getPluginDefinition();
$dependencies['module'][] = $definition['provider'];
return $dependencies;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$definition = $this
->getPluginDefinition();
$form['provider'] = [
'#type' => 'value',
'#value' => $definition['provider'],
];
$form += $this
->frontEndEnvironmentForm($form, $form_state);
return $form;
}
public function frontEndEnvironmentForm($form, FormStateInterface $form_state) {
return [];
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$this
->frontEndEnvironmentFormValidate($form, $form_state);
}
public function frontEndEnvironmentFormValidate($form, FormStateInterface $form_state) {
}
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
->frontEndEnvironmentSubmit($form, $form_state);
}
}
public function frontEndEnvironmentSubmit($form, FormStateInterface $form_state) {
}
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;
}
protected function transliteration() {
if (!$this->transliteration) {
$this->transliteration = \Drupal::transliteration();
}
return $this->transliteration;
}
public function setTransliteration(TransliterationInterface $transliteration) {
$this->transliteration = $transliteration;
}
public function deploymentWasTriggered(ResponseInterface $response) : bool {
return $response
->getStatusCode() === 200;
}
}