Commit c2b58306 authored by catch's avatar catch
Browse files

Issue #3523109 by ghost of drupal past, donquixote, nicxvan, dww, larowlan,...

Issue #3523109 by ghost of drupal past, donquixote, nicxvan, dww, larowlan, quietone: Remove support for extending #[Hook] and remove classes extending it #[FormAlter] and #[Preprocess]
parent 4cf0fed2
Loading
Loading
Loading
Loading
Loading
+0 −54
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Core\Hook\Attribute;

use Drupal\Core\Hook\Order\OrderInterface;

/**
 * Hook attribute for FormAlter.
 *
 * @see hook_form_alter().
 */
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class FormAlter extends Hook {

  /**
   * {@inheritdoc}
   */
  public const string PREFIX = 'form';

  /**
   * {@inheritdoc}
   */
  public const string SUFFIX = 'alter';

  /**
   * Constructs a FormAlter attribute object.
   *
   * @param string $form_id
   *   (optional) The ID of the form that this implementation alters.
   *   If this is left blank then `form_alter` is the hook that is registered.
   * @param string $method
   *   (optional) The method name. If this attribute is on a method, this
   *   parameter is not required. If this attribute is on a class and this
   *   parameter is omitted, the class must have an __invoke() method, which is
   *   taken as the hook implementation.
   * @param string|null $module
   *   (optional) The module this implementation is for. This allows one module
   *   to implement a hook on behalf of another module. Defaults to the module
   *   the implementation is in.
   * @param \Drupal\Core\Hook\Order\OrderInterface|null $order
   *   (optional) Set the order of the implementation.
   */
  public function __construct(
    string $form_id = '',
    public string $method = '',
    public ?string $module = NULL,
    public ?OrderInterface $order = NULL,
  ) {
    parent::__construct($form_id, $method, $module, $order);
  }

}
+2 −24
Original line number Diff line number Diff line
@@ -97,28 +97,11 @@
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class Hook implements HookAttributeInterface {

  /**
   * The hook prefix such as `form`.
   *
   * @var string
   */
  public const string PREFIX = '';

  /**
   * The hook suffix such as `alter`.
   *
   * @var string
   */
  public const string SUFFIX = '';

  /**
   * Constructs a Hook attribute object.
   *
   * @param string $hook
   *   The short hook name, without the 'hook_' prefix.
   *   $hook is only optional when Hook is extended and a PREFIX or SUFFIX is
   *   defined. When using the [#Hook] attribute directly $hook is required.
   *   See Drupal\Core\Hook\Attribute\Preprocess.
   * @param string $method
   *   (optional) The method name. If this attribute is on a method, this
   *   parameter is not required. If this attribute is on a class and this
@@ -132,15 +115,10 @@ class Hook implements HookAttributeInterface {
   *   (optional) Set the order of the implementation.
   */
  public function __construct(
    public string $hook = '',
    public string $hook,
    public string $method = '',
    public ?string $module = NULL,
    public ?OrderInterface $order = NULL,
  ) {
    $this->hook = implode('_', array_filter([static::PREFIX, $hook, static::SUFFIX]));
    if ($this->hook === '') {
      throw new \LogicException('The Hook attribute or an attribute extending the Hook attribute must provide the $hook parameter, a PREFIX or a SUFFIX.');
    }
  }
  ) {}

}
+0 −23
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Core\Hook\Attribute;

/**
 * Attribute for defining a class method as a preprocess function.
 *
 * Pass no arguments for hook_preprocess `#[Preprocess]`.
 * For `hook_preprocess_HOOK` pass the `HOOK` without the `hook_preprocess`
 * portion `#[Preprocess('HOOK')]`.
 *
 * See \Drupal\Core\Hook\Attribute\Hook for additional information.
 */
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class Preprocess extends Hook {
  /**
   * {@inheritdoc}
   */
  public const string PREFIX = 'preprocess';

}
+2 −2
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@

namespace Drupal\comment\Hook;

use Drupal\Core\Hook\Attribute\Preprocess;
use Drupal\Core\Hook\Attribute\Hook;

/**
 * Hook implementations for comment.
@@ -12,7 +12,7 @@ class CommentThemeHooks {
  /**
   * Implements hook_preprocess_HOOK() for block templates.
   */
  #[Preprocess('block')]
  #[Hook('preprocess_block')]
  public function preprocessBlock(&$variables): void {
    if ($variables['configuration']['provider'] == 'comment') {
      $variables['attributes']['role'] = 'navigation';
+2 −2
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@

namespace Drupal\comment_empty_title_test\Hook;

use Drupal\Core\Hook\Attribute\Preprocess;
use Drupal\Core\Hook\Attribute\Hook;

/**
 * Hook implementations for comment_empty_title_test.
@@ -14,7 +14,7 @@ class CommentEmptyTitleTestThemeHooks {
  /**
   * Implements hook_preprocess_comment().
   */
  #[Preprocess('comment')]
  #[Hook('preprocess_comment')]
  public function preprocessComment(&$variables): void {
    $variables['title'] = '';
  }
Loading