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

Issue #3501480 by phenaproxima, jurgenhaas: Create an action that can apply a recipe

parent ed2a889a
No related branches found
No related tags found
No related merge requests found
Pipeline #403041 canceled
<?php
namespace Drupal\eca_misc\Plugin\Action;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Recipe\Recipe;
use Drupal\Core\Recipe\RecipeRunner;
use Drupal\Core\Session\AccountInterface;
use Drupal\eca\Plugin\Action\ConfigurableActionBase;
/**
* Loads a query argument from the request into the token environment.
*
* @Action(
* id = "eca_apply_recipe",
* label = @Translation("Recipe: apply"),
* description = @Translation("Applies a given recipe."),
* eca_version_introduced = "2.1.2"
* )
*/
class ApplyRecipe extends ConfigurableActionBase {
/**
* {@inheritdoc}
*/
public function access($object, ?AccountInterface $account = NULL, $return_as_object = FALSE) {
$result = AccessResult::allowed();
$path = $this->tokenService->replace($this->configuration['recipe_path']);
if ($path === '') {
$result = AccessResult::forbidden('Path is empty.');
}
elseif (!is_dir($path)) {
$result = AccessResult::forbidden('Path does not exist.');
}
return $return_as_object ? $result : $result->isAllowed();
}
/**
* {@inheritdoc}
*/
public function execute(): void {
$path = trim($this->tokenService->replace($this->configuration['recipe_path']));
$path = trim(rtrim($path, '/'));
$recipe = Recipe::createFromDirectory($path);
RecipeRunner::processRecipe($recipe);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration(): array {
return [
'recipe_path' => '',
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
$form['recipe_path'] = [
'#type' => 'textfield',
'#title' => $this->t('Recipe path'),
'#default_value' => $this->configuration['recipe_path'],
'#required' => TRUE,
'#eca_token_replacement' => TRUE,
];
return parent::buildConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
$this->configuration['recipe_path'] = $form_state->getValue('recipe_path');
parent::submitConfigurationForm($form, $form_state);
}
}
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