marketo_suite-1.0.x-dev/src/Plugin/SubmissionBehaviorManager.php
src/Plugin/SubmissionBehaviorManager.php
<?php
namespace Drupal\e3_marketo\Plugin;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\SortArray;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
/**
* Plugin handler for Marketo submission behaviors.
*
* @ingroup e3_marketo
*/
class SubmissionBehaviorManager extends DefaultPluginManager {
/**
* Constructs an SubmissionBehaviorManager object.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct(
'Plugin/SubmissionBehavior',
$namespaces,
$module_handler,
'Drupal\e3_marketo\Plugin\SubmissionBehaviorInterface',
'Drupal\e3_marketo\Annotation\SubmissionBehavior'
);
$this->alterInfo('submission_behavior_info');
$this->setCacheBackend($cache_backend, 'submission_behaviors');
}
/**
* Get the behavior options.
*
* @param bool $load
* Set to TRUE to fully load the plugins.
*
* @return array
* Array of options.
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
*/
public function getBehaviorOptions(bool $load = FALSE) : array {
$options = [];
$definitions = $this->getDefinitions();
// Sort definitions by weight.
if ($definitions) {
uasort($definitions, [SortArray::class, 'sortByWeightElement']);
}
// Build results array.
foreach ($this->getDefinitions() as $plugin_id => $plugin_definition) {
if ($load) {
$options[$plugin_id] = $this->createInstance($plugin_definition['id'], $plugin_definition);
}
else {
$options[$plugin_id] = Html::escape($plugin_definition['label']);
}
}
return $options;
}
/**
* Run plugin alters for the Marketo form entity form.
*
* @param array $form
* Entity form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state.
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
*/
public function invokeEntityFormAlters(array $form, FormStateInterface $form_state): void {
/** @var \Drupal\e3_marketo\Plugin\SubmissionBehaviorBase[] $behaviors */
$behaviors = $this->getBehaviorOptions(TRUE);
foreach ($behaviors as $behavior) {
$behavior->alterMarketoFormEntityForm($form, $form_state);
}
}
}
