You are here

public function TableDragExampleRootLeafForm::buildForm in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 tabledrag_example/src/Form/TableDragExampleRootLeafForm.php \Drupal\tabledrag_example\Form\TableDragExampleRootLeafForm::buildForm()

Build the parent-child example form.

Tabledrag will take care of updating the parent_id relationship on each row of our table when we drag items around, but we need to build out the initial tree structure ourselves. This means ordering our items such that children items come directly after their parent items, and all items are sorted by weight relative to their siblings.

Parameters

array $form: Render array representing from.

\Drupal\Core\Form\FormStateInterface $form_state: Current form state.

Return value

array The render array defining the elements of the form.

Overrides FormInterface::buildForm

File

modules/tabledrag_example/src/Form/TableDragExampleRootLeafForm.php, line 84

Class

TableDragExampleRootLeafForm
Table drag example root leaf form.

Namespace

Drupal\tabledrag_example\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['description'] = [
    '#type' => 'item',
    '#markup' => $this
      ->t("Tabledrag rows can be marked as roots or leaves. This limits the way the user can interact with them in drag-and-drop operations. We'll mark some rows this way and you can try dragging them around on the page to see how they are limited."),
  ];
  $form['info'] = [
    '#markup' => '<ul>
        <li>' . $this
      ->t("Rows with the 'tabledrag-leaf' class cannot have child rows.") . '</li>
        <li>' . $this
      ->t("Rows with the 'tabledrag-root' class cannot be nested under a parent row.") . '</li></ul>',
  ];
  $form['table-row'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Name'),
      $this
        ->t('Description'),
      $this
        ->t('Weight'),
      $this
        ->t('Parent'),
    ],
    '#empty' => $this
      ->t('Sorry, There are no items!'),
    // 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' => 'match',
        'relationship' => 'parent',
        'group' => 'row-pid',
        'source' => 'row-id',
        'hidden' => TRUE,
        /* hides the WEIGHT & PARENT tree columns below */
        'limit' => FALSE,
      ],
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'row-weight',
      ],
    ],
  ];

  // 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 the renderer service as you would
  // everywhere else.
  $results = $this
    ->getData();
  foreach ($results as $row) {

    // TableDrag: Mark the table row as draggable.
    $form['table-row'][$row->id]['#attributes']['class'][] = 'draggable';

    // We can add the 'tabledrag-root' class to a row in order to indicate
    // that the row may not be nested under a parent row.  In our sample data
    // for this example, the description for the item with id '11' flags it as
    // a 'root' item which should not be nested.
    if ($row->id == '11') {
      $form['table-row'][$row->id]['#attributes']['class'][] = 'tabledrag-root';
    }

    // We can add the 'tabledrag-leaf' class to a row in order to indicate
    // that the row may not contain child rows.  In our sample data for this
    // example, the description for the item with id '12' flags it as a 'leaf'
    // item which can not contain child items.
    if ($row->id == '12') {
      $form['table-row'][$row->id]['#attributes']['class'][] = 'tabledrag-leaf';
    }

    // TableDrag: Sort the table row according to its existing/configured
    // weight.
    $form['table-row'][$row->id]['#weight'] = $row->weight;

    // Indent item on load.
    if (isset($row->depth) && $row->depth > 0) {
      $indentation = [
        '#theme' => 'indentation',
        '#size' => $row->depth,
      ];
    }

    // Some table columns containing raw markup.
    $form['table-row'][$row->id]['name'] = [
      '#markup' => $row->name,
      '#prefix' => !empty($indentation) ? $this->render
        ->render($indentation) : '',
    ];
    $form['table-row'][$row->id]['description'] = [
      '#type' => 'textfield',
      '#required' => TRUE,
      '#default_value' => $row->description,
    ];

    // This is hidden from #tabledrag array (above).
    // TableDrag: Weight column element.
    $form['table-row'][$row->id]['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight for ID @id', [
        '@id' => $row->id,
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $row->weight,
      // Classify the weight element for #tabledrag.
      '#attributes' => [
        'class' => [
          'row-weight',
        ],
      ],
    ];
    $form['table-row'][$row->id]['parent']['id'] = [
      '#parents' => [
        'table-row',
        $row->id,
        'id',
      ],
      '#type' => 'hidden',
      '#value' => $row->id,
      '#attributes' => [
        'class' => [
          'row-id',
        ],
      ],
    ];
    $form['table-row'][$row->id]['parent']['pid'] = [
      '#parents' => [
        'table-row',
        $row->id,
        'pid',
      ],
      '#type' => 'number',
      '#size' => 3,
      '#min' => 0,
      '#title' => $this
        ->t('Parent ID'),
      '#default_value' => $row->pid,
      '#attributes' => [
        'class' => [
          'row-pid',
        ],
      ],
    ];
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save All Changes'),
  ];
  $form['actions']['cancel'] = [
    '#type' => 'submit',
    '#value' => 'Cancel',
    '#attributes' => [
      'title' => $this
        ->t('Return to TableDrag Overview'),
    ],
    '#submit' => [
      '::cancel',
    ],
  ];
  return $form;
}