You are here

class DigestIntervalActionDeriver in Message Digest 8

Derives action plugins for changing digest intervals.

Hierarchy

Expanded class hierarchy of DigestIntervalActionDeriver

File

message_digest_ui/src/Plugin/Derivative/DigestIntervalActionDeriver.php, line 15

Namespace

Drupal\message_digest_ui\Plugin\Derivative
View source
class DigestIntervalActionDeriver extends DeriverBase implements ContainerDeriverInterface {
  use StringTranslationTrait;

  /**
   * The message notifier plugin manager.
   *
   * @var \Drupal\message_notify\Plugin\Notifier\Manager
   */
  protected $messageNotifier;

  /**
   * The message subscribe email manager service.
   *
   * @var \Drupal\message_subscribe_email\Manager
   */
  protected $subscribeManager;

  /**
   * Constructs the action deriver.
   *
   * @param \Drupal\message_notify\Plugin\Notifier\Manager $message_notifier
   *   The notifier plugin manager.
   */
  public function __construct(Manager $message_notifier) {
    $this->messageNotifier = $message_notifier;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static($container
      ->get('plugin.message_notify.notifier.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {

    // This is called very early during installation, and the flag service
    // is not completely ready, so these are hardcoded for the time being.
    $flags = [
      'email_node' => 'node',
      'email_term' => 'taxonomy_term',
      'email_user' => 'user',
    ];
    foreach ($flags as $flag_id => $entity_type_id) {

      // Create a set of actions for each email flag/entity combination.
      $plugin = [
        'type' => $entity_type_id,
      ];
      foreach ($this
        ->getDigestNotifiers() as $plugin_id => $label) {
        $id = $flag_id . ':' . ($plugin_id ?: 'immediate');
        $plugin['label'] = $label;
        $plugin['id'] = $id;
        $this->derivatives[$id] = $plugin + $base_plugin_definition;
      }
    }
    return parent::getDerivativeDefinitions($base_plugin_definition);
  }

  /**
   * Helper method to get digest notifiers.
   *
   * Also appends the 'send immediately' non-plugin.
   */
  protected function getDigestNotifiers() {
    $values = [
      $this
        ->t('Send immediately'),
    ];
    foreach ($this->messageNotifier
      ->getDefinitions() as $plugin_id => $plugin_definition) {

      // Strip off the prefix.
      $plugin_id = str_replace('message_digest:', '', $plugin_id);
      if (is_subclass_of($plugin_definition['class'], DigestInterface::class)) {
        $values[$plugin_id] = $plugin_definition['title'];
      }
    }
    return $values;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
DigestIntervalActionDeriver::$messageNotifier protected property The message notifier plugin manager.
DigestIntervalActionDeriver::$subscribeManager protected property The message subscribe email manager service.
DigestIntervalActionDeriver::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
DigestIntervalActionDeriver::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
DigestIntervalActionDeriver::getDigestNotifiers protected function Helper method to get digest notifiers.
DigestIntervalActionDeriver::__construct public function Constructs the action deriver.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.