Snippets
Created by
Daniel Sasser
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | <?php
namespace Drupal\rules\Form\Expression;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\rules\Ui\RulesUiHandlerTrait;
use Drupal\rules\Engine\ConditionExpressionContainerInterface;
use Drupal\rules\Engine\RulesComponent;
/**
* Form view structure for Rules condition containers.
*/
class ConditionContainerForm implements ExpressionFormInterface {
use ExpressionFormTrait;
use RulesUiHandlerTrait;
use StringTranslationTrait;
/**
* The rule expression object this form is for.
*
* @var \Drupal\rules\Engine\ConditionExpressionContainerInterface
*/
protected $conditionContainer;
protected $expressionManager;
/**
* Creates a new object of this class.
*/
public function __construct(ConditionExpressionContainerInterface $condition_container) {
$this->conditionContainer = $condition_container;
$this->expressionManager = \Drupal::service('plugin.manager.rules_expression');
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state, $options = []) {
// By default skip.
if (!empty($options['init'])) {
/* @var $handler \Drupal\rules\Ui\RulesUiConfigHandler */
$handler = $this->getRulesUiHandler();
/* @var $config \Drupal\rules\Entity\ReactionRuleConfig */
$config = $handler->getConfig();
$form['init_help'] = array(
'#type' => 'container',
'#id' => 'rules-plugin-add-help',
'content' => array(
'#markup' => t('You are about to add a new @plugin to the @config-plugin %label. Use indentation to make conditions a part of this logic group. See <a href="@url">the online documentation</a> for more information on condition sets.',
array(
'@plugin' => $this->conditionContainer->getLabel(),
'@config-plugin' => $config->bundle(),
'%label' => $config->label(),
'@url' => 'http://drupal.org/node/1300034',
)
),
),
'submit' => [
'#type' => 'submit',
'#value' => 'Continue',
],
);
}
else {
$form['conditions'] = [
'#type' => 'container',
];
$form['conditions']['table'] = [
'#type' => 'table',
'#caption' => $this->t('Conditions'),
'#header' => [
$this->t('Elements'),
[
'data' => $this->t('Elements'),
],
[
'data' => $this->t('Operations'),
'colspan' => 3,
],
],
'#attributes' => [
'id' => 'rules_conditions_table',
],
'#tabledrag' => [
[
'action' => 'match',
'relationship' => 'parent',
'group' => 'condition-parent',
'subgroup' => 'condition-parent',
'source' => 'condition-id',
],
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'condition-weight',
],
],
];
$form['conditions']['table']['#empty'] = $this->t('None');
// Get hold of conditions.
// @todo See if we can add getExpressions method of ExpressionContainerBase.
$conditions = [];
foreach ($this->conditionContainer as $condition) {
$conditions[] = $condition;
}
$depth = 0;
// Sort conditions by weight.
@uasort($conditions, [$this->conditionContainer, 'expressionSortHelper']);
foreach ($conditions as $condition) {
$this->buildRow($form, $form_state, $condition->getUuid(), $depth);
}
$this->buildFooter($form, $form_state);
}
return $form;
}
/**
* Build condition container table rows.
*
* @param array $form
* The form array.
* @param FormStateInterface $form_state
* The current form state.
* @param string $condition
* A condition expression uuid to add to the form table.
* @param int $depth
* The depth of the condition within the condition tree.
*/
private function buildRow(&$form, FormStateInterface &$form_state, $condition, $depth, $parent = NULL) {
$uuid = $condition;
$component = $this->getRulesUiHandler()->getComponent();
/* @var $rule_expression \Drupal\rules\Plugin\RulesExpression\Rule */
$rule_expression = $component->getExpression();
$condition = $rule_expression->getExpression($condition);
$row = &$form['conditions']['table'][$uuid];
// TableDrag: Mark the table row as draggable.
$row['#attributes']['class'][] = 'draggable';
$row['title'] = [
[
'#theme' => 'indentation',
'#size' => $depth,
],
[
'#markup' => $condition->getLabel(),
],
];
$row['weight'] = [
'#type' => 'weight',
'#delta' => 50,
'#default_value' => $condition->getWeight(),
'#attributes' => ['class' => ['condition-weight']],
];
// Operations (dropbutton) column.
$rules_ui_handler = $this->getRulesUiHandler();
$row['operations'] = [
'data' => [
'#type' => 'dropbutton',
'#links' => [
'edit' => [
'title' => $this->t('Edit'),
'url' => $rules_ui_handler->getUrlFromRoute('expression.edit', [
'uuid' => $condition->getUuid(),
]),
],
'delete' => [
'title' => $this->t('Delete'),
'url' => $rules_ui_handler->getUrlFromRoute('expression.delete', [
'uuid' => $condition->getUuid(),
]),
],
],
],
];
$row['parent'] = [
'#type' => 'hidden',
'#default_value' => $parent ? $parent->getUuid() : 0,
'#attributes' => ['class' => ['condition-parent']],
];
if ($condition->getPluginId() === 'rules_condition') {
$row['#attributes']['class'][] = 'tabledrag-leaf';
}
// TableDrag: Sort the table row according to its weight.
$row['#weight'] = $condition->getWeight();
$row['id'] = [
'#type' => 'hidden',
'#value' => $uuid,
'#attributes' => ['class' => ['condition-id']],
];
$row['plugin_id'] = [
'#type' => 'hidden',
'#default_value' => $condition->getPluginId(),
];
$configuration = $condition->getConfiguration();
if (!empty($configuration['conditions'])) {
$depth++;
foreach ($configuration['conditions'] as $child_condition) {
$this->buildRow($form, $form_state, $child_condition['uuid'], $depth, $condition);
}
}
}
/**
* Build condition container table footer.
*
* @param array $form
* The form array.
* @param FormStateInterface $form_state
* The current form state.
*/
private function buildFooter(&$form, FormStateInterface &$form_state) {
$footer = array(
'#prefix' => '<ul class="action-links">',
'#suffix' => '</ul>',
'links' => array(
array(
'#theme' => 'menu_local_action',
'#attribues' => ['classes' => ['action-links']],
'#link' => [
'title' => $this->t('Add condition'),
'url' => $this->getRulesUiHandler()->getUrlFromRoute('expression.add', [
'expression_id' => 'rules_condition',
]),
],
),
array(
'#theme' => 'menu_local_action',
'#link' => [
'title' => $this->t('Add AND'),
'url' => $this->getRulesUiHandler()->getUrlFromRoute('expression.add', [
'expression_id' => 'rules_and',
]),
],
),
array(
'#theme' => 'menu_local_action',
'#link' => [
'title' => $this->t('Add OR'),
'url' => $this->getRulesUiHandler()->getUrlFromRoute('expression.add', [
'expression_id' => 'rules_or',
]),
],
),
),
);
// Table footer.
$form['conditions']['table']['#footer'] = [
array(
'class' => array('footer-class'),
'data' => array(
array(
'data' => $footer,
'colspan' => 2,
),
),
),
];
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValue('table');
if ($values) {
$elements = $this->mirrorElements($values);
$tree = $this->buildElementTree($values, $elements, 'parent', 'id');
$this->saveElementTree($tree, $elements);
$handler = $this->getRulesUiHandler();
$component = $handler->getComponent();
/* @var $rule_expression \Drupal\rules\Plugin\RulesExpression\Rule */
$rule_expression = $component->getExpression();
// Remove the original expressions since we've cloned them to a new tree.
foreach ($values as $uuid => $old_expression) {
$rule_expression->deleteExpression($uuid);
}
$this->getRulesUiHandler()->updateComponent($component);
}
}
/**
* Build a tree of values from a flat array.
*
* @param array $values
* An associative array.
* @param string $pidKey
* The parent id key in the values.
* @param string $idKey
* The id key in the values.
*
* @return mixed
* A heirarchical tree of values.
*/
private function buildElementTree($values, &$elements, $pidKey, $idKey = NULL) {
$grouped = array();
foreach ($values as $sub) {
$grouped[$sub[$pidKey]][] = $sub;
}
$fnBuilder = function ($siblings) use (&$fnBuilder, &$elements, $grouped, $idKey, $elements) {
foreach ($siblings as $k => $sibling) {
$id = $sibling[$idKey];
if (isset($grouped[$id])) {
$sibling['conditions'] = $fnBuilder($grouped[$id]);
foreach ($sibling['conditions'] as $condition) {
// Tell the child who the parent is by setting its root.
$child = $elements[$condition[$idKey]];
// Add the child to the parent object.
$elements[$id]->addExpressionObject($child);
}
}
$siblings[$k] = $sibling;
}
return $siblings;
};
$tree = $fnBuilder($grouped[0]);
return $tree;
}
/**
* Creates "clones" of submitted elements for use in a new condition tree.
*
* @param array $values
* The array of form values.
*
* @return array|mixed
* The derived condition expression elements.
*/
private function mirrorElements($values) {
$elements = NULL;
$handler = $this->getRulesUiHandler();
$component = $handler->getComponent();
/* @var $rule_expression \Drupal\rules\Plugin\RulesExpression\Rule */
$rule_expression = $component->getExpression();
foreach ($values as $uuid => $element) {
$condition = $rule_expression->getExpression($element['id']);
$configuration = $condition->getConfiguration();
unset($configuration['uuid']);
$configuration['weight'] = $element['weight'];
$elements[$uuid] = $this->expressionManager->createInstance($condition->getPluginId(), $configuration);
}
return $elements;
}
/**
* Process and save expression condition tree.
*
* @param array $tree
* An associative array of expression elements.
*/
private function saveElementTree($tree, &$elements) {
$handler = $this->getRulesUiHandler();
$component = $handler->getComponent();
/* @var $rule_expression \Drupal\rules\Plugin\RulesExpression\Rule */
$rule_expression = $component->getExpression();
foreach ($tree as $branch) {
$rule_expression->addExpressionObject($elements[$branch['id']]);
}
$this->getRulesUiHandler()->updateComponent($component);
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.