Skip to content
Snippets Groups Projects
Commit 0d0d7546 authored by Jürgen Haas's avatar Jürgen Haas
Browse files

Issue #3501927: Add support for project browser

parent 142f980b
No related branches found
Tags 2.1.2
1 merge request!464Issue #3501927 by jurgenhaas: Add support for project browser
Pipeline #405240 passed with warnings
Showing with 346 additions and 0 deletions
name: 'ECA Project Browser'
type: module
description: 'Events and actions for the Project Browser.'
core_version_requirement: ^10.4 || ^11
package: ECA
dependencies:
- eca:eca
- project_browser:project_browser(^2)
<?php
/**
* @file
* ECA Misc submodule.
*/
use Drupal\eca_project_browser\HookHandler;
/**
* Helper function to return the hook handler service.
*
* @return \Drupal\eca_project_browser\HookHandler
* The hook handler service.
*/
function _eca_project_browser_hook_handler(): HookHandler {
return \Drupal::service('eca_project_browser.hook_handler');
}
/**
* Implements hook_project_browser_source_info_alter().
*/
function eca_project_browser_project_browser_source_info_alter(array &$definitions): void {
_eca_project_browser_hook_handler()->projectBrowserSourceInfo($definitions);
}
/**
* Implements hook_module_implements_alter().
*/
function eca_project_browser_module_implements_alter(array &$implementations, string $hook): void {
if ($hook == 'project_browser_source_info_alter') {
// Make sure that this hook gets called last.
$org = $implementations['eca_project_browser'];
unset($implementations['eca_project_browser']);
$implementations['eca_project_browser'] = $org;
}
}
services:
eca_project_browser.hook_handler:
class: Drupal\eca_project_browser\HookHandler
arguments: ['@eca.trigger_event']
<?php
namespace Drupal\eca_project_browser\Event;
/**
* Contains all events thrown in the project browser.
*/
class ProjectBrowserEvents {
/**
* Name of the event fired during project browser source plugin alter.
*
* @var string
*/
const string SOURCE_INFO_ALTER = 'source_info_alter';
}
<?php
namespace Drupal\eca_project_browser\Event;
use Symfony\Contracts\EventDispatcher\Event;
/**
* Event that is dispatched when project browser source plugin info alters.
*/
class ProjectBrowserSourceInfoAlterEvent extends Event {
/**
* Constructs the project browser source plugin info alter event.
*
* @param array $definitions
* The plugin definition.
*/
public function __construct(
protected array &$definitions,
) {}
/**
* Determines if the plugin with the given ID exists.
*
* @param string $id
* The plugin ID.
*
* @return bool
* TRUE, if the plugin exists, FALSE otherwise.
*/
public function pluginExists(string $id): bool {
return isset($this->definitions[$id]);
}
/**
* Sets a property of the source plugin info.
*
* @param string $id
* The plugin ID.
* @param string $key
* The property key.
* @param string $value
* The value of the property.
* @param bool $localTask
* TRUE, if the key should be set for the local task, FALSE if the property
* is a generic one.
*/
public function setProperty(string $id, string $key, string $value, bool $localTask = FALSE): void {
if (!$this->pluginExists($id)) {
return;
}
if ($localTask) {
$this->definitions[$id]['local_task'][$key] = $value;
}
else {
$this->definitions[$id][$key] = $value;
}
}
}
<?php
namespace Drupal\eca_project_browser;
use Drupal\eca\Event\BaseHookHandler;
/**
* The handler for hook implementations within the eca_misc.module file.
*
* @internal
* This class is not meant to be used as a public API. It is subject for name
* change or may be removed completely, also on minor version updates.
*/
class HookHandler extends BaseHookHandler {
/**
* Triggers the event for when project browser source info get altered.
*
* @param array $definitions
* The list of source plugin definitions.
*/
public function projectBrowserSourceInfo(array &$definitions): void {
$this->triggerEvent->dispatchFromPlugin('project_browser:source_info_alter', $definitions);
}
}
<?php
namespace Drupal\eca_project_browser\Plugin\Action;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\eca\Plugin\Action\ConfigurableActionBase;
use Drupal\eca_project_browser\Event\ProjectBrowserSourceInfoAlterEvent;
use Drupal\project_browser\Plugin\ProjectBrowserSourceManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Describes the eca_project_browser_source_plugin_info_alter action.
*
* @Action(
* id = "eca_project_browser_source_plugin_info_alter",
* label = @Translation("Project Browser: Alter source plugin info"),
* description = @Translation("Allows to change certain properties of source plugins."),
* eca_version_introduced = "2.1.2"
* )
*/
class AlterSourcePluginInfo extends ConfigurableActionBase {
/**
* The project browser source manager.
*
* @var \Drupal\project_browser\Plugin\ProjectBrowserSourceManager
*/
protected ProjectBrowserSourceManager $manager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->manager = $container->get(ProjectBrowserSourceManager::class);
return $instance;
}
/**
* {@inheritdoc}
*/
public function access($object, ?AccountInterface $account = NULL, $return_as_object = FALSE): bool|AccessResultInterface {
$access_result = AccessResult::forbidden();
$event = $this->getEvent();
if ($event instanceof ProjectBrowserSourceInfoAlterEvent) {
if ($event->pluginExists($this->configuration['plugin_id'])) {
$access_result = AccessResult::allowed();
}
}
return $return_as_object ? $access_result : $access_result->isAllowed();
}
/**
* {@inheritdoc}
*/
public function execute(): void {
/** @var \Drupal\eca_project_browser\Event\ProjectBrowserSourceInfoAlterEvent $event */
$event = $this->getEvent();
foreach ([
'label' => FALSE,
'description' => FALSE,
'title' => TRUE,
'weight' => TRUE,
] as $key => $localTask) {
$configKey = $localTask ? 'local_task_' . $key : $key;
if ($this->configuration[$configKey] !== '') {
$event->setProperty($this->configuration['plugin_id'], $key, $this->configuration[$configKey], $localTask);
}
}
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration(): array {
return [
'plugin_id' => '',
'label' => '',
'description' => '',
'local_task_title' => '',
'local_task_weight' => '',
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
$options = [];
foreach ($this->manager->getDefinitions() as $definition) {
$options[$definition['id']] = $definition['label'];
}
$form['plugin_id'] = [
'#type' => 'select',
'#title' => $this->t('Plugin'),
'#default_value' => $this->configuration['plugin_id'],
'#options' => $options,
'#required' => TRUE,
];
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#default_value' => $this->configuration['label'],
];
$form['description'] = [
'#type' => 'textfield',
'#title' => $this->t('Description'),
'#default_value' => $this->configuration['description'],
];
$form['local_task_title'] = [
'#type' => 'textfield',
'#title' => $this->t('Local task: title'),
'#default_value' => $this->configuration['local_task_title'],
];
$form['local_task_weight'] = [
'#type' => 'number',
'#title' => $this->t('Local task: weight'),
'#default_value' => $this->configuration['local_task_weight'],
];
return parent::buildConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
$this->configuration['plugin_id'] = $form_state->getValue('plugin_id');
$this->configuration['label'] = $form_state->getValue('label');
$this->configuration['description'] = $form_state->getValue('description');
$this->configuration['local_task_title'] = $form_state->getValue('local_task_title');
$this->configuration['local_task_weight'] = $form_state->getValue('local_task_weight');
parent::submitConfigurationForm($form, $form_state);
}
}
<?php
namespace Drupal\eca_project_browser\Plugin\ECA\Event;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\eca\Plugin\ECA\Event\EventBase;
use Drupal\eca_project_browser\Event\ProjectBrowserEvents;
use Drupal\eca_project_browser\Event\ProjectBrowserSourceInfoAlterEvent;
/**
* Plugin implementation of the ECA Events for project browser.
*
* @EcaEvent(
* id = "project_browser",
* deriver = "Drupal\eca_project_browser\Plugin\ECA\Event\ProjectBrowserEventDeriver",
* eca_version_introduced = "2.1.2"
* )
*/
class ProjectBrowserEvent extends EventBase {
/**
* {@inheritdoc}
*/
public static function definitions(): array {
return [
'source_info_alter' => [
'label' => 'Alter source plugin info',
'event_name' => ProjectBrowserEvents::SOURCE_INFO_ALTER,
'event_class' => ProjectBrowserSourceInfoAlterEvent::class,
'description' => new TranslatableMarkup('Fires during project browser source plugin alter.'),
],
];
}
}
<?php
namespace Drupal\eca_project_browser\Plugin\ECA\Event;
use Drupal\eca\Plugin\ECA\Event\EventDeriverBase;
/**
* Deriver for ECA Project Browser event plugins.
*/
class ProjectBrowserEventDeriver extends EventDeriverBase {
/**
* {@inheritdoc}
*/
protected function definitions(): array {
return ProjectBrowserEvent::definitions();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment