public function RulesListForm::buildForm in Custom filter 2.0.x
Create the form that list the rules.
Overrides FormInterface::buildForm
File
- src/
Form/ RulesListForm.php, line 35
Class
- RulesListForm
- Defines a form to list the rules.
Namespace
Drupal\customfilter\FormCode
public function buildForm(array $form, FormStateInterface $form_state, CustomFilter $customfilter = NULL) {
$this->entity = $customfilter;
$form['rules'] = [
'#type' => 'table',
'#tree' => 'true',
'#header' => [
$this
->t('Rule'),
$this
->t('Machine name'),
[
'data' => $this
->t('Enabled'),
'class' => [
'checkbox',
],
],
$this
->t('Weight'),
$this
->t('Parent'),
[
'data' => $this
->t('Operations'),
'colspan' => 2,
],
],
'#empty' => $this
->t('There are no items yet. <a href="@add-url">Add an item.</a>', [
'@add-url' => Url::fromRoute('customfilter.rules.add', [
'customfilter' => $this->entity
->id(),
])
->toString(),
]),
// TableDrag: Each array value is a list of callback arguments for
// drupal_add_tabledrag(). The #id of the table is automatically
// prepended if there is none, an HTML ID is auto-generated.
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'rules-order-weight',
],
[
'action' => 'match',
'relationship' => 'parent',
'group' => 'rule-prid',
'subgroup' => 'rule-prid',
'source' => 'rule-rid',
'hidden' => TRUE,
],
],
];
// Build the table rows and columns.
// The first nested level in the render array forms the table row, on which
// you likely want to set #attributes and #weight.
// Each child element on the second level represents a table column cell in
// the respective table row, which are render elements on their own. For
// single output elements, use the table cell itself for the render element.
// If a cell should contain multiple elements, simply use nested sub-keys
// to build the render element structure for drupal_render() as you would
// everywhere else.
$entities = $this->entity
->getRules('', TRUE);
if (count($entities) > 0) {
$form['fid'] = [
'#type' => 'hidden',
'#value' => $this->entity
->id(),
];
}
$maxWeight = count($this->entity->rules);
$this
->rulesTree($form, $entities, $maxWeight, 0);
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save changes'),
];
return $form;
}