summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordpi2018-02-25 15:26:54 (GMT)
committerdpi2018-02-25 15:26:54 (GMT)
commit6584e9a6ff85cd3eb9fe83202b398595c1cc8c39 (patch)
tree99f141fa7d0fe4d1b60bc73b76b75a017c7a9254
parent6caafca6a71bfd3ef10bc66a39ec8114d86525e8 (diff)
Added form alterations service.
-rw-r--r--coi.services.yml4
-rw-r--r--src/CoiFormAlterations.php121
-rw-r--r--src/CoiFormAlterationsInterface.php20
3 files changed, 145 insertions, 0 deletions
diff --git a/coi.services.yml b/coi.services.yml
new file mode 100644
index 0000000..8faeb57
--- /dev/null
+++ b/coi.services.yml
@@ -0,0 +1,4 @@
+services:
+ coi.form_alterations:
+ class: Drupal\coi\CoiFormAlterations
+ arguments: ['@config.factory']
diff --git a/src/CoiFormAlterations.php b/src/CoiFormAlterations.php
new file mode 100644
index 0000000..65cf4d9
--- /dev/null
+++ b/src/CoiFormAlterations.php
@@ -0,0 +1,121 @@
+<?php
+
+namespace Drupal\coi;
+
+use Drupal\Component\Utility\Html;
+use Drupal\Core\Config\ConfigFactoryInterface;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Render\Element;
+use Drupal\Core\StringTranslation\StringTranslationTrait;
+
+/**
+ * Form alterations for COI.
+ */
+class CoiFormAlterations implements CoiFormAlterationsInterface {
+
+ use StringTranslationTrait;
+
+ /**
+ * The config factory.
+ *
+ * @var \Drupal\Core\Config\ConfigFactoryInterface
+ */
+ protected $configFactory;
+
+ /**
+ * Constructs a new CoiFormAlterations object.
+ *
+ * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
+ * The config factory.
+ */
+ public function __construct(ConfigFactoryInterface $config_factory) {
+ $this->configFactory = $config_factory;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function hookFormAlter(array &$form, FormStateInterface $form_state, string $form_id) : void {
+ // Unfortunately we cannot modify (disable) the element in element #process,
+ // as it happens too late in FormBuilder (right after, in
+ // handleInputElement), so we have to use form alters.
+ $this->alterTree($form);
+ }
+
+ /**
+ * Recursively looks for form elements with COI keys.
+ *
+ * @param array $elements
+ * A render array.
+ */
+ protected function alterTree(array &$elements) : void {
+ foreach (Element::children($elements) as $key) {
+ $element = &$elements[$key];
+
+ $coiSettings = $this->configFactory->get('coi.settings');
+ $overrideBehavior = $coiSettings->get('override_behavior');
+
+ $modifyElement = !empty($overrideBehavior) && $overrideBehavior !== CoiValues::OVERRIDE_BEHAVIOUR_NONE;
+
+ // If already disabled.
+ $access = isset($element['#access']) ? $element['#access'] : TRUE;
+ if (!$access || !empty($element['#disabled']) || !$modifyElement) {
+ continue;
+ }
+
+ $this->alterTree($element);
+
+ if (!isset($element['#config'])) {
+ continue;
+ }
+
+ $elementConfig = $element['#config'];
+ list($configBin, $configKey) = $elementConfig['config'];
+
+ $config = $this->configFactory->get($configBin);
+ $hasOverrides = $config->hasOverrides($configKey);
+
+ // Selectors.
+ // Add selectors regardless of whether the element is overridden.
+ if ($coiSettings->get('styling.selectors')) {
+ $configBinClass = str_replace('.', '-', $configBin);
+ $configKeyClass = str_replace('.', '-', $configKey);
+ $element['#attributes']['class'][] = Html::getClass('config');
+ if ($hasOverrides) {
+ $element['#attributes']['class'][] = Html::getClass('config--overridden');
+ }
+ $element['#attributes']['class'][] = Html::getClass('config--' . $configBinClass);
+ $element['#attributes']['class'][] = Html::getClass('config--' . $configBinClass . '--' . $configKeyClass);
+ }
+
+ if (!$hasOverrides) {
+ continue;
+ }
+
+ // Can see override value, and not secret or can always see secrets.
+ if ($coiSettings->get('overridden_value.enabled') && (empty($elementConfig['secret']) || $coiSettings->get('overridden_value.secrets'))) {
+ $value = $config->get($configKey);
+ }
+ else {
+ $value = $this->t('- Overridden value -');
+ }
+
+ if ($overrideBehavior == CoiValues::OVERRIDE_BEHAVIOUR_DISABLE) {
+ $element['#disabled'] = TRUE;
+ if ($coiSettings->get('overridden_value.element')) {
+ $element['#default_value'] = $value;
+ }
+ }
+ else if ($overrideBehavior == CoiValues::OVERRIDE_BEHAVIOUR_NO_ACCESS) {
+ $element['#access'] = FALSE;
+ }
+
+ // Message.
+ if ($coiSettings->get('message.enabled')) {
+ $message = $coiSettings->get('message.template');
+ $element['#coi_override_message'] = $message;
+ }
+ }
+ }
+
+}
diff --git a/src/CoiFormAlterationsInterface.php b/src/CoiFormAlterationsInterface.php
new file mode 100644
index 0000000..6763145
--- /dev/null
+++ b/src/CoiFormAlterationsInterface.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace Drupal\coi;
+
+use Drupal\Core\Form\FormStateInterface;
+
+/**
+ * Interface for form alterations for COI.
+ */
+interface CoiFormAlterationsInterface {
+
+ /**
+ * Implements hook_form_alter().
+ *
+ * @see \hook_form_alter()
+ * @see \coi_form_alter()
+ */
+ public function hookFormAlter(array &$form, FormStateInterface $form_state, string $form_id) : void;
+
+}