marketo_suite-1.0.x-dev/src/Plugin/MarketoHandlerBase.php
src/Plugin/MarketoHandlerBase.php
<?php
namespace Drupal\e3_marketo\Plugin;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\Renderer;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\e3_marketo\Entity\MarketoFormEntityInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Base class for Marketo Handler plugins.
*
* @ingroup e3_marketo
*/
abstract class MarketoHandlerBase extends PluginBase implements MarketoHandlerInterface {
use StringTranslationTrait;
/**
* Marketo settings.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected ImmutableConfig $marketoConfig;
/**
* Marketo settings. This is needed for chain-loading.
*
* @var array
*/
public static array $loadedMarketoSettings;
/**
* Constructs a MarketoHandlerBase object.
*
* @param array $configuration
* Plugin Configuration.
* @param string $plugin_id
* Plugin ID.
* @param mixed $plugin_definition
* Plugin Definition.
* @param \Drupal\Core\Database\Connection $database
* Database Connection.
* @param \Drupal\Core\Config\ConfigFactory $configFactory
* Config Factory.
* @param \Drupal\Core\Session\AccountInterface $currentUser
* Current User.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity Type Manager.
* @param \Drupal\Core\Render\Renderer $renderer
* Renderer service.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* Messenger service.
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* Request Stack.
* @param \Drupal\e3_marketo\Plugin\SubmissionBehaviorManager $submissionBehaviorManager
* Submission behavior manager.
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* Route match.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
protected Connection $database,
protected ConfigFactory $configFactory,
protected AccountInterface $currentUser,
protected EntityTypeManagerInterface $entityTypeManager,
protected Renderer $renderer,
protected MessengerInterface $messenger,
protected RequestStack $requestStack,
protected SubmissionBehaviorManager $submissionBehaviorManager,
protected RouteMatchInterface $routeMatch
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->marketoConfig = $configFactory->get('e3_marketo.settings');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('database'),
$container->get('config.factory'),
$container->get('current_user'),
$container->get('entity_type.manager'),
$container->get('renderer'),
$container->get('messenger'),
$container->get('request_stack'),
$container->get('plugin.manager.submission_behavior_manager'),
$container->get('current_route_match'),
);
}
/**
* {@inheritdoc}
*/
public function getLabel(): string {
return $this->pluginDefinition['label'];
}
/**
* {@inheritdoc}
*/
public function getDescription(): string {
return $this->pluginDefinition['description'] ?? '';
}
/**
* {@inheritdoc}
*/
public function alterMarketoEntityForm(MarketoFormEntityInterface $marketo_form, array &$form) {
return NULL;
}
/**
* {@inheritdoc}
*/
public function applies(MarketoFormEntityInterface $marketo_form, string $callback, mixed &$arguments = NULL): bool {
// Make sure selected callback exists within the handler.
if (method_exists($this, $callback)) {
return TRUE;
}
else {
return FALSE;
}
}
}
