You are here

public function ReorderFolderForm::buildForm in Taxonomy File Tree 3.x

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

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/ReorderFolderForm.php, line 93

Class

ReorderFolderForm
Term reordering form.

Namespace

Drupal\tft\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, TermInterface $taxonomy_term = NULL) {
  $tid = $taxonomy_term
    ->id();
  $tree = _tft_folder_tree($tid);
  $root_depth = _tft_get_depth($tid) + 1;
  $form['#attributes'] = [
    'id' => 'tft-manage-folders-form',
  ];
  $form['#tid'] = $tid;
  $form['#use_hierarchy'] = TRUE;
  $form['#use_weight'] = TRUE;
  $this
    ->manage_folders_form($tree, $form, $root_depth);
  $data = array_filter($form['table'], function ($item, $key) {
    return is_array($item) && preg_match('/term-[0-9]+/', $key);
  }, ARRAY_FILTER_USE_BOTH);
  $form['table'] = [
    '#type' => 'table',
    '#attributes' => [
      'id' => 'tft-outline',
    ],
    '#header' => [
      $this
        ->t('Name'),
      $this
        ->t('Parent'),
      $this
        ->t('Weight'),
    ],
    '#tabledrag' => [
      [
        'action' => 'match',
        'relationship' => 'parent',
        'group' => 'taxonomy_term_hierarchy-parent',
        'subgroup' => 'taxonomy_term_hierarchy-parent',
        'source' => 'taxonomy_term_hierarchy-tid',
      ],
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'taxonomy_term_hierarchy-weight',
      ],
    ],
    '#tree' => TRUE,
    '#weight' => 1,
  ];
  foreach ($data as $key => $item) {
    $form['table'][$key]['#attributes']['class'][] = 'draggable';
    $form['table'][$key]['#weight'] = $item['weight']['#default_value'];
    $form['table'][$key]['name'] = [
      [
        '#theme' => 'indentation',
        '#size' => isset($item['depth']['#value']) ? $item['depth']['#value'] : 0,
      ],
      [
        '#theme' => 'image',
        '#uri' => drupal_get_path('module', 'tft') . '/img/folder.png',
        '#attributes' => [
          'class' => 'tft-admin-folder-content-item',
        ],
      ],
      $item['name'],
    ];
    $form['table'][$key]['parent'] = [
      $item['parent'],
      $item['id'],
      $item['type'],
    ];
    $form['table'][$key]['weight'] = [
      $item['weight'],
    ];
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save changes'),
  ];
  $form[] = [
    // Add CSS and Javascript files.
    '#attached' => [
      'library' => [
        'tft/tft',
      ],
    ],
  ];
  return $form;
}