RulesUiConfigHandler.php 5.05 KB
Newer Older
1 2
<?php

3
namespace Drupal\rules\Ui;
4

5
use Drupal\Core\Config\ConfigFactoryInterface;
6 7 8 9 10 11
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\rules\Engine\RulesComponent;
12
use Drupal\rules\Form\EmbeddedComponentForm;
13 14 15 16 17 18 19
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * The default handler for RulesUi plugins that store to config.
 *
 * It follows a list of supported settings. Note that settings that are not
 * marked as optional are required.
20 21
 * - config_parameter: The name of the routing parameter holding a config
 *   object providing the edited component. The parameter object must implement
22
 *   \Drupal\rules\Ui\RulesUiComponentProviderInterface. Required, unless
23 24 25 26 27 28 29
 *   config_name and config_key are provided.
 * - config_name: The name of a (simple) configuration object containing the
 *   configuration data of the edited component. For example,
 *   'your_module.your_config'. Required if 'config_parameter' is omitted.
 * - config_key: The key used to get/set the configuration of the edited
 *   component. For example, 'conditions' or 'foo.conditions'. Required if
 *   'config_parameter' is omitted.
30
 *
TR's avatar
TR committed
31
 * @see \Drupal\rules\Ui\RulesUiDefinition::settings()
32 33 34 35 36 37 38
 */
class RulesUiConfigHandler extends PluginBase implements RulesUiHandlerInterface, ContainerFactoryPluginInterface {
  use TempStoreTrait;

  /**
   * The rules UI (plugin) definition.
   *
39
   * @var \Drupal\rules\Ui\RulesUiDefinition
40 41 42 43 44 45 46 47 48 49
   */
  protected $pluginDefinition;

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $currentRouteMatch;

50 51 52 53 54 55 56
  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

57 58 59 60
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
61
    return new static($configuration, $plugin_id, $plugin_definition, $container->get('current_route_match'), $container->get('config.factory'));
62 63 64 65 66
  }

  /**
   * {@inheritdoc}
   */
67
  public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, ConfigFactoryInterface $config_factory) {
68 69
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->currentRouteMatch = $route_match;
70
    $this->configFactory = $config_factory;
71 72 73 74 75
  }

  /**
   * Gets the edited config object.
   *
76
   * @return \Drupal\rules\Ui\RulesUiComponentProviderInterface|\Drupal\Core\Config\Config
77 78
   *   The component provider object (usually a config entity) or the editable
   *   config object.
79 80 81 82
   */
  public function getConfig() {
    $config = $this->fetchFromTempStore();
    if (!$config) {
83 84 85 86 87 88
      if (isset($this->pluginDefinition->settings['config_parameter'])) {
        $config = $this->currentRouteMatch->getParameter($this->pluginDefinition->settings['config_parameter']);
      }
      else {
        $config = $this->configFactory->getEditable($this->pluginDefinition->settings['config_name']);
      }
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    }
    return $config;
  }

  /**
   * {@inheritdoc}
   */
  public function getComponentLabel() {
    if (isset($this->pluginDefinition->component_label)) {
      return $this->pluginDefinition->component_label;
    }
    elseif ($this->getConfig() instanceof EntityInterface) {
      return $this->getConfig()->label();
    }
    else {
      return $this->pluginDefinition->component_type_label;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getComponent() {
112 113 114 115 116 117
    $config = $this->getConfig();
    if ($config instanceof RulesUiComponentProviderInterface) {
      return $config->getComponent();
    }
    else {
      $configuration = $config->get($this->pluginDefinition->settings['config_key']);
118
      return RulesComponent::createFromConfiguration($configuration);
119
    }
120 121 122 123 124 125 126
  }

  /**
   * {@inheritdoc}
   */
  public function updateComponent(RulesComponent $component) {
    $config = $this->getConfig();
127 128 129 130
    if ($config instanceof RulesUiComponentProviderInterface) {
      $config->updateFromComponent($component);
    }
    else {
131
      $config->set($this->pluginDefinition->settings['config_key'], $component->getConfiguration());
132
    }
133 134 135 136 137 138
    $this->storeToTempStore($config);
  }

  /**
   * {@inheritdoc}
   */
139
  public function getBaseRouteUrl(array $options = []) {
TR's avatar
TR committed
140
    // @see \Drupal\Core\Url::fromRouteMatch()
141 142 143 144 145 146 147 148 149 150 151
    return Url::fromRoute(
      $this->pluginDefinition->base_route,
      $this->currentRouteMatch->getRawParameters()->all(),
      $options
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getUrlFromRoute($route_suffix, array $route_parameters, array $options = []) {
TR's avatar
TR committed
152
    // @see \Drupal\Core\Url::fromRouteMatch()
153 154 155 156 157 158 159 160
    return Url::fromRoute(
      $this->pluginDefinition->base_route . '.' . $route_suffix,
      $route_parameters + $this->currentRouteMatch->getRawParameters()->all(),
      $options
    );
  }

  /**
161
   * {@inheritdoc}
162
   */
163 164
  public function getForm() {
    return new EmbeddedComponentForm($this);
165 166 167
  }

}