You are here

public function GridActionForm::buildForm in Filebrowser 3.x

Same name and namespace in other branches
  1. 8.2 src/Form/GridActionForm.php \Drupal\filebrowser\Form\GridActionForm::buildForm()

Parameters

array $params: Required data to build the for: Associative array keyed 'actions', 'node', 'data'

Overrides FormInterface::buildForm

File

src/Form/GridActionForm.php, line 92

Class

GridActionForm
Class GridActionForm.

Namespace

Drupal\filebrowser\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $items = null, $params = null) {
  $this->common = \Drupal::service('filebrowser.common');
  $this->helper = \Drupal::service('form.helper');
  $this->node = $params['node'];
  $this->nid = $this->node
    ->id();
  $this->relativeFid = empty($params['data']['fid']) ? 0 : $params['data']['fid'];
  $form = [];
  $this->helper
    ->initForm($form, $params['node']);
  $form['#tree'] = TRUE;
  $form['#attached']['library'][] = 'views/views.module';
  $column_width = round(100 / $items['options']['columns']) - 1;

  // Create the form action elements
  // If user has permissions that needs a button or checkbox then has_actions will be true.
  $has_actions = !empty($params['actions']);
  if ($has_actions) {

    // Create the bar containing the action buttons
    $this->helper
      ->createActionBar($form, $params['actions'], $this->relativeFid);

    // Create a "Select all" checkbox
    $form['select_all'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Select all'),
      '#title_display' => 'before',
      '#ajax' => [
        'callback' => [
          $this,
          'selectAll',
        ],
        'progress' => 'none',
      ],
    ];
  }

  // main container
  $form['container'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => [
        'filebrowser-checkboxes-container',
      ],
      'class' => [
        'horizontal',
        'clearfix',
      ],
    ],
  ];

  // rows (table-row)
  foreach ($items['grid_items'] as $row_nr => $row) {
    $row_name = 'row_' . $row_nr;
    $form['container'][$row_name] = [
      '#type' => 'container',
      '#options' => '',
      '#attributes' => [
        'class' => [
          'filebrowser-grid-row',
        ],
      ],
    ];

    //columns (td)
    foreach ($row['row'] as $col_nr => $content) {
      $col_name = 'col_' . $col_nr;
      $form['container'][$row_name][$col_name] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            Common::FILEBROWSER_GRID_CONTAINER_COLUMN_CLASS,
          ],
          'width' => $column_width,
        ],
      ];
      if ($has_actions) {
        $form['container'][$row_name][$col_name][$content['content']['file']->fid] = [
          '#type' => 'checkbox',
          '#title' => '',
          '#attributes' => [
            'class' => [
              'filebrowser-checkbox',
            ],
          ],
          '#field_suffix' => $content['content']['grid'],
        ];
      }
      else {
        $form['container'][$row_name][$col_name][$content['content']['file']->fid] = [
          '#markup' => render($content['content']['grid']),
        ];
      }
    }
  }
  return $form;
}