You are here

private function RulesListForm::rulesTree in Custom filter 2.0.x

Add the rules to table.

1 call to RulesListForm::rulesTree()
RulesListForm::buildForm in src/Form/RulesListForm.php
Create the form that list the rules.

File

src/Form/RulesListForm.php, line 110

Class

RulesListForm
Defines a form to list the rules.

Namespace

Drupal\customfilter\Form

Code

private function rulesTree(array &$form, array $rules, $maxWeight, $level = 0) {
  foreach ($rules as $rule) {

    // TableDrag: Mark the table row as draggable.
    $form['rules'][$rule['rid']]['#attributes']['class'][] = 'draggable';

    // TableDrag: Sort the table row according to its
    // existing/configured weight.
    $form['rules'][$rule['rid']]['#weight'] = $rule['weight'];

    // Some table columns containing raw markup.
    $form['rules'][$rule['rid']]['name'] = [
      [
        '#theme' => 'indentation',
        '#size' => $level,
      ],
      [
        '#plain_text' => $rule['name'],
      ],
    ];
    $form['rules'][$rule['rid']]['ridss'] = [
      '#markup' => $rule['rid'],
    ];
    $form['rules'][$rule['rid']]['enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable @title menu link', [
        '@title' => $rule['name'],
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $rule['enabled'],
    ];

    // TableDrag: Weight column element.
    $form['rules'][$rule['rid']]['weight'] = [
      '#type' => 'weight',
      '#delta' => $maxWeight,
      '#title' => $this
        ->t('Weight for @title', [
        '@title' => $rule['name'],
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $rule['weight'],
      // Classify the weight element for #tabledrag.
      '#attributes' => [
        'class' => [
          'rules-order-weight',
        ],
      ],
    ];
    $form['rules'][$rule['rid']]['prid'] = [
      '#type' => 'textfield',
      '#size' => '12',
      '#default_value' => $rule['prid'],
      '#attributes' => [
        'class' => [
          'rule-prid',
        ],
      ],
    ];

    // Operations (dropbutton) column.
    $links = [];
    $links['edit'] = [
      'title' => $this
        ->t('Edit'),
      'url' => Url::fromRoute('customfilter.rules.edit', [
        'customfilter' => $this->entity
          ->id(),
        'rule_id' => $rule['rid'],
      ]),
    ];
    $links['add_sub_rule'] = [
      'title' => $this
        ->t('Add Sub Rule'),
      'url' => Url::fromRoute('customfilter.rules.add.subrule', [
        'customfilter' => $this->entity
          ->id(),
        'rule_id' => $rule['rid'],
      ]),
    ];
    $links['delete'] = [
      'title' => $this
        ->t('Delete'),
      'url' => Url::fromRoute('customfilter.rules.delete', [
        'customfilter' => $this->entity
          ->id(),
        'rule_id' => $rule['rid'],
      ]),
    ];
    $form['rules'][$rule['rid']]['operations'] = [
      '#type' => 'operations',
      '#links' => $links,
    ];
    $form['rules'][$rule['rid']]['rid'] = [
      '#type' => 'hidden',
      '#value' => $rule['rid'],
      '#attributes' => [
        'class' => [
          'rule-rid',
        ],
      ],
    ];
    $subrules = $this->entity
      ->getRules($rule['rid'], TRUE);
    if (count($subrules) > 0) {
      $this
        ->rulesTree($form, $subrules, $maxWeight, $level + 1);
    }
  }
}