View source
<?php
namespace Drupal\business_rules\Plugin\BusinessRulesVariable;
use Drupal\Core\Render\Markup;
use Drupal\Core\Render\RenderContext;
use Drupal\business_rules\Entity\Variable;
use Drupal\business_rules\Events\BusinessRulesEvent;
use Drupal\business_rules\ItemInterface;
use Drupal\business_rules\Plugin\BusinessRulesVariablePlugin;
use Drupal\business_rules\VariableObject;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\views\Views;
class ViewResultVariable extends BusinessRulesVariablePlugin {
protected $bundleInfo;
protected $entityFieldManager;
public function __construct(array $configuration = [], $plugin_id = 'view_result_variable', $plugin_definition = []) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityFieldManager = $this->util->container
->get('entity_field.manager');
$this->bundleInfo = $this->util->container
->get('entity_type.bundle.info');
}
public function getSettingsForm(array &$form, FormStateInterface $form_state, ItemInterface $item) {
$settings['view'] = [
'#type' => 'select',
'#title' => t('View to execute. View name : Display mode id : Display mode title.'),
'#options' => $this->util
->getViewsOptions(),
'#required' => TRUE,
'#default_value' => $item
->getSettings('view'),
'#description' => t("Select the view to get the results. When you use the view fields, it will always have the raw value."),
];
$settings['view_html'] = [
'#title' => t('Use HTML rendered view fields'),
'#type' => 'checkbox',
'#default_value' => $item
->getSettings('view_html'),
'#description' => t('If checked, changes on the view fields formatter will reflect into the variables and HTML markup will be used.'),
];
$settings['arguments'] = [
'#type' => 'textarea',
'#title' => t('Arguments'),
'#description' => t('Any argument the view may need, one per line. Be aware of including them at same order as the CONTEXTUAL FILTERS configured in the view. You may use variables.'),
'#default_value' => $item
->getSettings('arguments'),
];
$form['#attached']['library'][] = 'business_rules/style';
return $settings;
}
public function changeDetails(Variable $variable, array &$row) {
$content = $this
->variableFields($variable);
$keyvalue = $this->util
->getKeyValueExpirable('view_result_variable');
$keyvalue
->set('variableFields.' . $variable
->id(), $content);
$details_link = Link::createFromRoute(t('Click here to see the view fields'), 'business_rules.ajax.modal', [
'method' => 'nojs',
'title' => t('View fields'),
'collection' => 'view_result_variable',
'key' => 'variableFields.' . $variable
->id(),
], [
'attributes' => [
'class' => [
'use-ajax',
],
],
])
->toString();
$row['description']['data']['#markup'] .= '<br>' . $details_link;
}
public function variableFields(Variable $variable) {
$defined_view = $variable
->getSettings('view');
$defined_view = explode(':', $defined_view);
$view_id = $defined_view[0];
$display = $defined_view[1];
$view = Views::getView($view_id);
$view
->setDisplay($display);
$view
->preExecute();
$view
->build();
$fields = $view->field;
$header = [
'variable' => t('Variable'),
'field' => t('Field'),
'type' => t('Type'),
];
$rows = [];
if (count($fields)) {
foreach ($fields as $field_name => $field) {
$field_id = $field->field;
$definition = $field->definition;
$entity_type = $definition['entity_type'];
if ($field
->getBaseId() == 'field') {
$bundles = $this->bundleInfo
->getBundleInfo($entity_type);
$bundles = array_keys($bundles);
$found = FALSE;
$idx_bundle = 0;
while (!$found && $idx_bundle < count($bundles)) {
$bundle = $bundles[$idx_bundle];
$fields_definitions = $this->entityFieldManager
->getFieldDefinitions($entity_type, $bundle);
$field_definition = isset($fields_definitions[$field_name]) ? $fields_definitions[$field_name] : NULL;
if (empty($field_definition)) {
$idx_bundle++;
}
else {
$field_type = $field_definition
->getType();
$found = TRUE;
}
}
$rows[] = [
'variable' => [
'data' => [
'#markup' => '{{' . $variable
->id() . '->' . $field_id . '}}',
],
],
'field' => [
'data' => [
'#markup' => $field
->label() ? $field
->label() : $field->realField,
],
],
'type' => [
'data' => [
'#markup' => $field_type,
],
],
];
}
}
}
else {
$rows[] = [
'data' => [
'#markup' => t('This view has no fields.'),
],
'colspan' => 3,
];
}
$content['help'] = [
'#type' => 'markup',
'#markup' => t('Notice that as this items are arrays, you only can use this variable values on items inside an action type: "Loop through a view result variable".'),
];
$content['variable_fields'] = [
'#type' => 'table',
'#rows' => $rows,
'#header' => $header,
'#sticky' => TRUE,
];
return $content;
}
public function evaluate(Variable $variable, BusinessRulesEvent $event) {
$defined_view = $variable
->getSettings('view');
$view_fields = $variable
->getSettings('view_html');
$args = $variable
->getSettings('arguments');
$event_variables = $event
->getArgument('variables');
$defined_view = explode(':', $defined_view);
$view_id = $defined_view[0];
$display = $defined_view[1];
$args = explode(chr(10), $args);
$args = array_map('trim', $args);
$args = array_filter($args, 'strlen');
foreach ($args as $key => $value) {
$args[$key] = $this
->processVariables($value, $event_variables);
}
$view = Views::getView($view_id);
$view
->setArguments($args);
$view
->setDisplay($display);
$view
->preExecute();
$view
->build();
$fields = $view->field;
$variableObject = NULL;
if ($view
->execute()) {
$renderer = \Drupal::service('renderer');
$view_result = $view->result;
$values = [];
foreach ($view_result as $key => $resultRow) {
if ($view_fields == TRUE) {
$inner_values = [];
$output = $renderer
->executeInRenderContext(new RenderContext(), function () use ($inner_values, $resultRow, $fields) {
foreach ($fields as $field) {
$field_id = $field->field;
$html_rended = Markup::create($field
->advancedRender($resultRow));
if (!is_string($html_rended)) {
$html_rended = $html_rended
->__toString();
}
$inner_values[$field_id] = $html_rended;
}
return $inner_values;
});
$values[$key] = $output;
}
else {
if ($view_fields == TRUE) {
$inner_values = [];
$output = $renderer
->executeInRenderContext(new RenderContext(), function () use ($inner_values, $resultRow, $fields) {
foreach ($fields as $field) {
$field_id = $field->field;
$html_rended = Markup::create($field
->advancedRender($resultRow));
if (!is_string($html_rended)) {
$html_rended = $html_rended
->__toString();
}
$inner_values[$field_id] = $html_rended;
}
return $inner_values;
});
$values[$key] = $output;
}
else {
foreach ($fields as $field) {
$field_id = $field->field;
$values[$key][$field_id] = $field
->getValue($resultRow);
}
}
}
}
$variableObject = new VariableObject($variable
->id(), $values, $variable
->getType());
}
else {
$this->util->logger
->error('View %view could not be executed. Arguments: %args', [
'%view' => $defined_view,
'%args' => implode(', ', $args),
]);
}
return $variableObject;
}
}