Created
November 13, 2012 17:25
-
-
Save EclipseGc/4067136 to your computer and use it in GitHub Desktop.
Actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\action\Plugin; | |
use Drupal\Component\Plugin\PluginBase; | |
abstract class ActionBase extends PluginBase implements ActionInterface { | |
/** | |
* Implements ActionInterface::setParameter(). | |
*/ | |
public function setParameter($key, $value) { | |
$definition = $this->getDefinition(); | |
if (!isset($definition['parameters'][$key])) { | |
throw new ActionException("The $key parameter is not valid for this action."); | |
} | |
if (!data_type_comparison($definition['parameters'][$key]['type'], $value)) { | |
throw new ActionException("The value passed for the $key parameter is not of the appropriate type."); | |
} | |
$this->configuration['parameters'][$key] = $value; | |
return $this; | |
} | |
/** | |
* Implements ActionInterface::getParameter(). | |
*/ | |
public function getParameter($key) { | |
$definition = $this->getDefinition(); | |
if (!isset($definition['parameters'][$key])) { | |
throw new ActionException("The $key parameter is not valid for this action."); | |
} | |
return $this->configuration['parameters'][$key]; | |
} | |
/** | |
* Implements ActionInterface::getParameters(). | |
*/ | |
public function getParameters() { | |
return $this->configuration['parameters']; | |
} | |
/** | |
* Implements ActionInterface::setProcessor(). | |
*/ | |
public function setProcessor($plugin_id, array $configuration) { | |
$this->configuration['processors'][] = system_plugin_manager('processor')->createInstance($plugin_id, $configuration); | |
return $this; | |
} | |
/** | |
* Implements ActionInterface::getProcessor(). | |
*/ | |
public function getProcessors() { | |
return $this->configuration['processors']; | |
} | |
/** | |
* Abstract definition of ActionInterface::execute(). | |
*/ | |
abstract function execute(); | |
} | |
use Drupal\Core\Annotation\Plugin; | |
use Drupal\Core\Annotation\Translation; | |
/** | |
* @Plugin( | |
* id = "send_email", | |
* parameters = | |
* ) | |
*/ | |
class ActionEmail extends ActionBase { | |
public function execute() { | |
$recipient = token_replace($this->configuration['recipient'], $this->configuration['parameters']); | |
// If the recipient is a registered user with a language preference, use | |
// the recipient's preferred language. Otherwise, use the system default | |
// language. | |
$recipient_account = user_load_by_mail($recipient); | |
if ($recipient_account) { | |
$langcode = user_preferred_langcode($recipient_account); | |
} | |
else { | |
$langcode = language_default()->langcode; | |
} | |
$params = array('context' => $this->configuration); | |
if (drupal_mail('system', 'action_send_email', $recipient, $langcode, $params)) { | |
watchdog('action', 'Sent email to %recipient', array('%recipient' => $recipient)); | |
} | |
else { | |
watchdog('error', 'Unable to send email to %recipient', array('%recipient' => $recipient)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment