Commit ca0393c0 authored by dpi's avatar dpi
Browse files

Issue #3310524 by dpi: Drupal 10 readiness

parent 5c7fc485
Loading
Loading
Loading
Loading
+12 −4
Changes for README.md: 12 added lines, 4 removed lines.
Original line number Diff line number Diff line
Config Override Inspector

Copyright (C) 2018 Daniel Phin (@dpi)
Copyright (C) 2022 Daniel Phin (@dpi)

# License

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+3 −4
Changes for coi.info.yml: 3 added lines, 4 removed lines.
Original line number Diff line number Diff line
name: 'Config Override Inspector'
type: module
description: ''
core_version_requirement: ^9
php: 7.3
core_version_requirement: '>=10'
php: 8.0
package: 'Administration'
configure: coi.settings
dependencies:
  - drupal:system (>=9.0.x)
  - config_override_core_fields:config_override_core_fields (>=3.0.x)
  - config_override_core_fields:config_override_core_fields (>=4.0.x)
+22 −8
Changes for coi.module: 22 added lines, 8 removed lines.
Original line number Diff line number Diff line
<?php

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\BubbleableMetadata;

/**
 * @file
 * Contains hooks for Config Override Inspector.
 */

declare(strict_types=1);

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\BubbleableMetadata;

/**
 * Implements hook_module_implements_alter().
 */
function coi_module_implements_alter(array &$implementations, string $hook) {
function coi_module_implements_alter(array &$implementations, string $hook): void {
  if ($hook == 'form_alter') {
    if (!isset($implementations['config_override_core_fields'])) {
      return;
@@ -105,14 +107,26 @@ function coi_token_info() : array {

/**
 * Implements hook_tokens().
 *
 * @param string $type
 *   Type.
 * @param array $tokens
 *   Tokens.
 * @param array{'coi':array{'active-value': mixed, 'overridden-value': mixed}} $data
 *   Data.
 * @param array $options
 *   Options
 * @param \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata
 *   Metadata.
 *
 * @return array
 *   Replacements.
 */
function coi_tokens(string $type, array $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata): array {
  $replacements = [];

  if ($type == 'coi' && !empty($data['coi'])) {
    /** @var array $values */
    $values = $data['coi'];

  if ($type === 'coi') {
    $values = $data['coi'] ?? throw new \LogicException('Missing token values for COI.');
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'active-value':
+3 −3
Changes for composer.json: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -18,9 +18,9 @@
    }
  ],
  "require": {
    "php": ">=7.3",
    "drupal/core": "^9",
    "drupal/config_override_core_fields": "^3"
    "php": ">=8.1",
    "drupal/core": "^10",
    "drupal/config_override_core_fields": "^4"
  },
  "suggest": {
    "drupal/token": "Shows Token UI on COI settings form."
+16 −32
Changes for src/CoiFormAlterations.php: 16 added lines, 32 removed lines.
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\coi;

use Drupal\Component\Utility\Html;
@@ -12,35 +14,22 @@ use Drupal\Core\Utility\Token;
/**
 * Form alterations for COI.
 */
class CoiFormAlterations implements CoiFormAlterationsInterface {
final class CoiFormAlterations implements CoiFormAlterationsInterface {

  use StringTranslationTrait;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The token service.
   *
   * @var \Drupal\Core\Utility\Token
   */
  protected $token;

  /**
   * Constructs a new CoiFormAlterations object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The config factory.
   * @param \Drupal\Core\Utility\Token $token
   *   The token service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, Token $token) {
    $this->configFactory = $config_factory;
    $this->token = $token;
  public function __construct(
    protected ConfigFactoryInterface $configFactory,
    protected Token $token,
  ) {
  }

  /**
@@ -67,7 +56,7 @@ class CoiFormAlterations implements CoiFormAlterationsInterface {
      $overrideBehavior = $coiSettings->get('override_behavior');

      // If already disabled.
      $access = isset($element['#access']) ? $element['#access'] : TRUE;
      $access = $element['#access'] ?? TRUE;
      if (!$access || !empty($element['#disabled'])) {
        continue;
      }
@@ -81,8 +70,8 @@ class CoiFormAlterations implements CoiFormAlterationsInterface {
        continue;
      }

      $elementConfig = isset($element['#config']) ? $element['#config'] : $element['#config_data_store'];
      list($configBin, $configKey) = explode(':', $elementConfig['key']);
      $elementConfig = $element['#config'] ?? $element['#config_data_store'];
      [$configBin, $configKey] = explode(':', $elementConfig['key']);

      $config = $this->configFactory->get($configBin);
      $hasOverrides = $config->hasOverrides($configKey);
@@ -105,12 +94,9 @@ class CoiFormAlterations implements CoiFormAlterationsInterface {
      }

      // 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 -');
      }
      $value = ($coiSettings->get('overridden_value.enabled') && (empty($elementConfig['secret']) || $coiSettings->get('overridden_value.secrets')))
        ? $config->get($configKey)
        : $this->t('- Overridden value -');

      if ($overrideBehavior == CoiValues::OVERRIDE_BEHAVIOUR_DISABLE) {
        $element['#disabled'] = TRUE;
@@ -126,11 +112,9 @@ class CoiFormAlterations implements CoiFormAlterationsInterface {
      if ($coiSettings->get('message.enabled')) {
        $message = $coiSettings->get('message.template');
        $tokens = [];
        $tokens['coi']['active-value'] = $config
          ->getOriginal($configKey, FALSE);
        $tokens['coi']['active-value'] = $config->getOriginal($configKey, FALSE);
        $tokens['coi']['overridden-value'] = $value;
        $element['#coi_override_message'] = $this->token
          ->replace($message, $tokens);;
        $element['#coi_override_message'] = $this->token->replace($message, $tokens);
      }
    }
  }
Loading