Skip to content

Commit

Permalink
Merge pull request #341 from esmero/ISSUE-340
Browse files Browse the repository at this point in the history
ISSUE-340: New JMESPath expression conditional for blocks
  • Loading branch information
DiegoPino authored Jul 27, 2023
2 parents c033697 + 0ff87b1 commit 974bb2f
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 3 deletions.
182 changes: 182 additions & 0 deletions src/Plugin/Condition/AdoJmesPath.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php

namespace Drupal\format_strawberryfield\Plugin\Condition;

use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\strawberryfield\StrawberryfieldUtilityServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides the 'ADO Type' condition.
*
* @Condition(
* id = "ado_jmespath_condition",
* label = @Translation("ADO JMESPath"),
* context_definitions = {
* "node" = @ContextDefinition("entity:node", label = @Translation("node"))
* }
* )
*/
class AdoJmesPath extends ConditionPluginBase implements ContainerFactoryPluginInterface {

/**
* @var \Drupal\strawberryfield\StrawberryfieldUtilityServiceInterface
*/
private StrawberryfieldUtilityServiceInterface $strawberryfieldUtilityService;

/**
* Creates a new AdoType instance.
*
* @param array $configuration
* The plugin configuration.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\strawberryfield\StrawberryfieldUtilityServiceInterface $strawberryfieldUtilityService
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, StrawberryfieldUtilityServiceInterface $strawberryfieldUtilityService) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->strawberryfieldUtilityService = $strawberryfieldUtilityService;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('strawberryfield.utility')
);
}

/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['ado_jmespaths'] = [
'#title' => $this->pluginDefinition['label'],
'#description' => t(
'Enter one or more ADO JMESPath expressions, <strong>one per line</strong>. Any non empty/not FALSE return (except an array with FALSE values) ,of the evaluated expresion(s), is considered a match',
),
'#type' => 'textarea',
'#default_value' => implode(PHP_EOL, $this->configuration['ado_jmespaths']),
];
return parent::buildConfigurationForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['ado_jmespaths'] = array_filter(array_map('trim', explode(PHP_EOL, $form_state->getValue('ado_jmespaths'))));
parent::submitConfigurationForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function evaluate() {
// Returns true if no ado types are selected and negate option is disabled.
if (empty($this->configuration['ado_jmespaths']) && !$this->isNegated()) {
return TRUE;
}
/** @var \Drupal\node\Entity\Node $node */
$entity = $this->getContextValue('node');

$matches = FALSE;
if ($entity && $sbf_fields = $this->strawberryfieldUtilityService->bearsStrawberryfield($entity)) {
foreach ($sbf_fields as $field_name) {
/* @var \Drupal\strawberryfield\Plugin\Field\FieldType\StrawberryFieldItem $field */
$field = $entity->get($field_name);
if (!$field->isEmpty()) {
foreach ($field->getIterator() as $delta => $itemfield) {
/** @var \Drupal\strawberryfield\Plugin\Field\FieldType\StrawberryFieldItem $itemfield */
foreach ($this->configuration['ado_jmespaths'] as $jmespath) {
if (!$matches && !empty($jmespath)) {
try {
$match_result = $itemfield->searchPath($jmespath);
if (is_array($match_result)) {
// Allows JMESPATHS that return [false] to be considered empty
$match_result = array_filter($match_result);
}
$matches = !empty($match_result);
}
catch (\Exception $e) {
$this->messenger()->addWarning('The JMESPath Block condition for Block @id is using an invalid JMESPath expression. Please correct the configuration.',[
'@id' => $this->getPluginId()
]);
}
}
}
}
}
}
}
else {
// This not an entity type that bears a strawberryfield. Therefore, this condition can not apply.
// However, it could still be negated and cause in a "TRUE" evaluation to flip and result in the "FALSE"
// being returned. Deal with that by removing the negation.
unset($this->configuration['negate']);
return TRUE;
}
// Default, return not matched.
return $matches;
}


/**
* {@inheritdoc}
*/
// TODO: This seems to require javascript to display, though it seems pretty superfluous.
public function summary() {
if (count($this->configuration['ado_jmespaths']) > 1) {
$ado_jmespaths = $this->configuration['ado_jmespaths'];
$last = array_pop($ado_jmespaths);
$ado_jmespaths = implode(', ', $ado_jmespaths);

if (empty($this->configuration['negate'])) {
return $this->t('@jmespath is @ado_jmespaths or @last', [
'@jmespath' => $this->pluginDefinition['label'],
'@ado_jmespaths' => $ado_jmespaths,
'@last' => $last,
]);
}
else {
return $this->t('@jmespath is not @ado_jmespaths or @last', [
'@type' => $this->pluginDefinition['label'],
'@ado_jmespaths' => $ado_jmespaths,
'@last' => $last,
]);
}
}
$ado_jmespath = reset($this->configuration['ado_jmespaths']);

if (empty($this->configuration['negate'])) {
return $this->t('@jmespath is @ado_jmespath', [
'@jmespath' => $this->pluginDefinition['label'],
'@ado_jmespath' => $ado_jmespath,
]);
}
else {
return $this->t('@jmespath is not @ado_jmespath', [
'@jmespath' => $this->pluginDefinition['label'],
'@ado_jmespath' => $ado_jmespath,
]);
}
}

/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'ado_jmespaths' => [],
] + parent::defaultConfiguration();
}

}
11 changes: 8 additions & 3 deletions src/Plugin/Condition/AdoType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\strawberryfield\StrawberryfieldUtilityService;
use Drupal\strawberryfield\StrawberryfieldUtilityServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand All @@ -22,6 +22,11 @@
*/
class AdoType extends ConditionPluginBase implements ContainerFactoryPluginInterface {


/**
* @var \Drupal\strawberryfield\StrawberryfieldUtilityServiceInterface
*/
private StrawberryfieldUtilityServiceInterface $strawberryfieldUtilityService;
/**
* Creates a new AdoType instance.
*
Expand All @@ -31,9 +36,9 @@ class AdoType extends ConditionPluginBase implements ContainerFactoryPluginInter
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\strawberryfield\StrawberryfieldUtilityService $strawberryfieldUtilityService
* @param \Drupal\strawberryfield\StrawberryfieldUtilityServiceInterface $strawberryfieldUtilityService
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, StrawberryfieldUtilityService $strawberryfieldUtilityService) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, StrawberryfieldUtilityServiceInterface $strawberryfieldUtilityService) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->strawberryfieldUtilityService = $strawberryfieldUtilityService;
}
Expand Down

0 comments on commit 974bb2f

Please sign in to comment.