transmuter-1.0.0-alpha2/src/Transmuter.php

src/Transmuter.php
<?php

namespace Drupal\transmuter;

use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\transmuter\Plugin\TransmuterManagerInterface;

/**
 * Invokable class which applies a series of element transmutations.
 */
class Transmuter implements \Stringable {

  /**
   * The transmuter plugin manager instance.
   *
   * @var \Drupal\transmuter\Plugin\TransmuterManagerInterface
   */
  protected static TransmuterManagerInterface $transmuterManager;

  /**
   * The theme hook this functor is run for.
   *
   * @var string
   */
  protected string $hook;

  /**
   * The list of transmuter plugin IDs which are invoked by this transmuter.
   *
   * @var string[]
   */
  protected array $pluginIds;

  /**
   * Gets the current transmuter plugin manager instance.
   *
   * @return \Drupal\transmuter\Plugin\TransmuterManagerInterface
   *   The transmuter plugin manager instance to use.
   */
  protected static function getTransmuterManager(): TransmuterManagerInterface {
    if (!isset(self::$transmuterManager)) {
      self::$transmuterManager = \Drupal::service('plugin.manager.transmuter');
    }

    return self::$transmuterManager;
  }

  /**
   * Creates a new instance of the  invokable object.
   *
   * @param string $hook
   *   The theme hook this element transmuter is meant to work on.
   * @param string[] $preprocessIds
   *   The list of IDs to execute preprocess plugins for when invoked.
   */
  public function __construct(string $hook, array $preprocessIds) {
    $this->hook = $hook;
    $this->pluginIds = $preprocessIds;
  }

  /**
   * The string value is the theme hook name this functor is invoked for.
   *
   * Creating a string following the "hook_preprocess_HOOK" callback convention,
   * with "transmuter" for the module, allows it to have the same precedence as
   * a theme preprocess hook defined from the transmuter module.
   *
   * This helps with sorting and grouping preprocess functions when altering
   * the theme registry.
   *
   * @return string
   *   The hook theme preprocess function name if this was a theme hook.
   */
  public function __toString(): string {
    return 'transmuter_preprocess_' . $this->hook;
  }

  /**
   * Get the theme hook name this functor is invoked for.
   *
   * @return string
   *   The hook this transmuter is set to preprocess.
   */
  public function getHook(): string {
    return $this->hook;
  }

  /**
   * Method for invoking this transmuter plugins.
   *
   * Method is called during the preprocessing stage of theme element rendering
   * and applies the preprocess() method of the assigned transmuter plugins in
   * the order the same order they are listed in the $pluginIds property.
   *
   * @param array $variables
   *   A reference to the theme element variables being transmuted.
   * @param string $hook
   *   The string name of the preprocess hook that is being worked on.
   * @param array $theme_info
   *   The theme definition for the element being processed. Information
   *   includes the template files, preprocess and process callbacks.
   */
  public function __invoke(array &$variables, string $hook, array $theme_info = []): void {
    $transmuterManager = static::getTransmuterManager();

    foreach ($this->pluginIds as $pluginId) {
      try {
        $transmuterManager
          ->createInstance($pluginId)
          ->preprocess($variables, $hook, $theme_info);
      }
      catch (PluginNotFoundException $e) {
        // Plugin is missing because provider is probably uninstalled, but
        // the callback was still registered. It should be safe to just skip
        // this processor.
        //
        // Unlikely to happen, since cache rebuilds are part of module
        // or theme uninstalls and this generally should not be out of sync.
      }
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc