Skip to content
Snippets Groups Projects
Commit 25d9ef4c authored by jurgenhaas's avatar jurgenhaas
Browse files

Add patch to eca

parent a4e33421
No related branches found
No related tags found
1 merge request!328Merging develop into main
Pipeline #1330373 passed
......@@ -101,6 +101,9 @@
"drupal/easy_email": {
"#3470506 Config schema": "https://gitlab.lakedrops.com/composer/plugin/drupal-environment/-/raw/main/patches/d10/3470506.diff"
},
"drupal/eca": {
"#3480137 Get list of bundles": "https://gitlab.lakedrops.com/composer/plugin/drupal-environment/-/raw/main/patches/d10/3480137.diff"
},
"drupal/entity_import": {
"#3061935 Make it work without content_translation": "https://gitlab.lakedrops.com/composer/plugin/drupal-environment/-/raw/main/patches/d10/3061935.diff"
},
......
diff --git a/modules/content/src/Plugin/Action/GetBundleList.php b/modules/content/src/Plugin/Action/GetBundleList.php
new file mode 100644
index 0000000000000000000000000000000000000000..4f715e5b4bdb7d621b3188acf697f602c6e5ce62
--- /dev/null
+++ b/modules/content/src/Plugin/Action/GetBundleList.php
@@ -0,0 +1,116 @@
+<?php
+
+namespace Drupal\eca_content\Plugin\Action;
+
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\eca\Plugin\Action\ConfigurableActionBase;
+use Drupal\eca\Plugin\ECA\PluginFormTrait;
+use Drupal\eca\Service\ContentEntityTypes;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * Get a list of bundles.
+ *
+ * @Action(
+ * id = "eca_get_bundle_list",
+ * label = @Translation("Entity: get list of bundles"),
+ * description = @Translation("Gets the list of bundles for a given entity type."),
+ * eca_version_introduced = "2.1.0"
+ * )
+ */
+class GetBundleList extends ConfigurableActionBase {
+
+ use PluginFormTrait;
+
+ /**
+ * The entity type service.
+ *
+ * @var \Drupal\eca\Service\ContentEntityTypes
+ */
+ protected ContentEntityTypes $entityTypes;
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
+ $plugin = parent::create($container, $configuration, $plugin_id, $plugin_definition);
+ $plugin->entityTypes = $container->get('eca.service.content_entity_types');
+ return $plugin;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function defaultConfiguration(): array {
+ return [
+ 'token_name' => '',
+ 'type' => '',
+ 'mode' => 'ids',
+ ] + parent::defaultConfiguration();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
+ $form['token_name'] = [
+ '#type' => 'textfield',
+ '#title' => $this->t('Name of token'),
+ '#default_value' => $this->configuration['token_name'],
+ '#description' => $this->t('Provide the name of a token that holds the bundle list.'),
+ '#weight' => -60,
+ '#required' => TRUE,
+ '#eca_token_reference' => TRUE,
+ ];
+ $options = $this->entityTypes->getTypes();
+ $form['type'] = [
+ '#type' => 'select',
+ '#title' => $this->t('Type'),
+ '#options' => $options,
+ '#default_value' => $this->configuration['type'],
+ '#description' => $this->t('The entity type for which to receive the list of bundles.'),
+ '#weight' => -50,
+ '#eca_token_select_option' => TRUE,
+ ];
+ $form['mode'] = [
+ '#type' => 'select',
+ '#title' => $this->t('Mode'),
+ '#options' => [
+ 'ids' => $this->t('IDs'),
+ 'labels' => $this->t('ILabels'),
+ ],
+ '#default_value' => $this->configuration['mode'],
+ '#description' => $this->t('This either returns a list of bundle IDs or of their labels.'),
+ '#weight' => -40,
+ ];
+ return parent::buildConfigurationForm($form, $form_state);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
+ $this->configuration['token_name'] = $form_state->getValue('token_name');
+ $this->configuration['type'] = $form_state->getValue('type');
+ $this->configuration['mode'] = $form_state->getValue('mode');
+ parent::submitConfigurationForm($form, $form_state);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function execute(): void {
+ if ($this->configuration['type'] === '_eca_token') {
+ $type = $this->getTokenValue('type', '');
+ }
+ else {
+ $type = $this->configuration['type'];
+ }
+ $bundles = $this->entityTypes->getBundles($type);
+ if ($this->configuration['mode'] === 'ids') {
+ $bundles = array_keys($bundles);
+ }
+ $this->tokenService->addTokenData($this->configuration['token_name'], $bundles);
+ }
+
+}
diff --git a/src/Service/ContentEntityTypes.php b/src/Service/ContentEntityTypes.php
index e8889ea735346d39c6441c17c635e0653c37f750..75e2aa51d7297946e0f6f93f07570ad53791c26e 100644
--- a/src/Service/ContentEntityTypes.php
+++ b/src/Service/ContentEntityTypes.php
@@ -119,6 +119,37 @@ class ContentEntityTypes {
return $this->typesAndBundles[$idx1][$idx2];
}
+ /**
+ * Gets the entity types.
+ *
+ * @return array
+ * The entity types.
+ */
+ public function getTypes(): array {
+ $result = [];
+ foreach ($this->entityTypeManager->getDefinitions() as $definition) {
+ if ($definition instanceof ContentEntityTypeInterface) {
+ $result[$definition->id()] = $definition->getLabel();
+ }
+ }
+ return $result;
+ }
+
+ /**
+ * Gets the bundles of an entity type.
+ *
+ * @return array
+ * The bundles of the entity type.
+ */
+ public function getBundles(string $entity_type_id): array {
+ $result = [];
+ $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type_id);
+ foreach ($bundles as $bundle => $bundleDef) {
+ $result[$bundle] = $bundleDef['label'];
+ }
+ return $result;
+ }
+
/**
* Gets the type and bundles.
*
......@@ -97,6 +97,9 @@
"drupal/easy_email": {
"#3470506 Config schema": "https://gitlab.lakedrops.com/composer/plugin/drupal-environment/-/raw/main/patches/d10/3470506.diff"
},
"drupal/eca": {
"#3480137 Get list of bundles": "https://gitlab.lakedrops.com/composer/plugin/drupal-environment/-/raw/main/patches/d10/3480137.diff"
},
"drupal/entity_import": {
"#3061935 Make it work without content_translation": "https://gitlab.lakedrops.com/composer/plugin/drupal-environment/-/raw/main/patches/d10/3061935.diff"
},
......
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