You are here

public function Table::buildOptionsForm in Views (for Drupal 7) 8.3

Render the given style.

Overrides StylePluginBase::buildOptionsForm

File

lib/Drupal/views/Plugin/views/style/Table.php, line 198
Definition of Drupal\views\Plugin\views\style\Table.

Class

Table
Style plugin to render each item as a row in a table.

Namespace

Drupal\views\Plugin\views\style

Code

public function buildOptionsForm(&$form, &$form_state) {
  parent::buildOptionsForm($form, $form_state);
  $handlers = $this->displayHandler
    ->getHandlers('field');
  if (empty($handlers)) {
    $form['error_markup'] = array(
      '#markup' => '<div class="error messages">' . t('You need at least one field before you can configure your table settings') . '</div>',
    );
    return;
  }
  $form['override'] = array(
    '#type' => 'checkbox',
    '#title' => t('Override normal sorting if click sorting is used'),
    '#default_value' => !empty($this->options['override']),
  );
  $form['sticky'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable Drupal style "sticky" table headers (Javascript)'),
    '#default_value' => !empty($this->options['sticky']),
    '#description' => t('(Sticky header effects will not be active for preview below, only on live output.)'),
  );
  $form['summary'] = array(
    '#type' => 'textfield',
    '#title' => t('Table summary'),
    '#description' => t('This value will be displayed as table-summary attribute in the html. Set this for better accessiblity of your site.'),
    '#default_value' => $this->options['summary'],
    '#maxlength' => 255,
  );

  // Note: views UI registers this theme handler on our behalf. Your module
  // will have to register your theme handlers if you do stuff like this.
  $form['#theme'] = 'views_ui_style_plugin_table';
  $columns = $this
    ->sanitize_columns($this->options['columns']);

  // Create an array of allowed columns from the data we know:
  $field_names = $this->displayHandler
    ->getFieldLabels();
  if (isset($this->options['default'])) {
    $default = $this->options['default'];
    if (!isset($columns[$default])) {
      $default = -1;
    }
  }
  else {
    $default = -1;
  }
  foreach ($columns as $field => $column) {
    $column_selector = ':input[name="style_options[columns][' . $field . ']"]';
    $form['columns'][$field] = array(
      '#type' => 'select',
      '#options' => $field_names,
      '#default_value' => $column,
    );
    if ($handlers[$field]
      ->click_sortable()) {
      $form['info'][$field]['sortable'] = array(
        '#type' => 'checkbox',
        '#default_value' => !empty($this->options['info'][$field]['sortable']),
        '#states' => array(
          'visible' => array(
            $column_selector => array(
              'value' => $field,
            ),
          ),
        ),
      );
      $form['info'][$field]['default_sort_order'] = array(
        '#type' => 'select',
        '#options' => array(
          'asc' => t('Ascending'),
          'desc' => t('Descending'),
        ),
        '#default_value' => !empty($this->options['info'][$field]['default_sort_order']) ? $this->options['info'][$field]['default_sort_order'] : 'asc',
        '#states' => array(
          'visible' => array(
            $column_selector => array(
              'value' => $field,
            ),
            ':input[name="style_options[info][' . $field . '][sortable]"]' => array(
              'checked' => TRUE,
            ),
          ),
        ),
      );

      // Provide an ID so we can have such things.
      $radio_id = drupal_html_id('edit-default-' . $field);
      $form['default'][$field] = array(
        '#type' => 'radio',
        '#return_value' => $field,
        '#parents' => array(
          'style_options',
          'default',
        ),
        '#id' => $radio_id,
        // because 'radio' doesn't fully support '#id' =(
        '#attributes' => array(
          'id' => $radio_id,
        ),
        '#default_value' => $default,
        '#states' => array(
          'visible' => array(
            $column_selector => array(
              'value' => $field,
            ),
          ),
        ),
      );
    }
    $form['info'][$field]['align'] = array(
      '#type' => 'select',
      '#default_value' => !empty($this->options['info'][$field]['align']) ? $this->options['info'][$field]['align'] : '',
      '#options' => array(
        '' => t('None'),
        'views-align-left' => t('Left'),
        'views-align-center' => t('Center'),
        'views-align-right' => t('Right'),
      ),
      '#states' => array(
        'visible' => array(
          $column_selector => array(
            'value' => $field,
          ),
        ),
      ),
    );
    $form['info'][$field]['separator'] = array(
      '#type' => 'textfield',
      '#size' => 10,
      '#default_value' => isset($this->options['info'][$field]['separator']) ? $this->options['info'][$field]['separator'] : '',
      '#states' => array(
        'visible' => array(
          $column_selector => array(
            'value' => $field,
          ),
        ),
      ),
    );
    $form['info'][$field]['empty_column'] = array(
      '#type' => 'checkbox',
      '#default_value' => isset($this->options['info'][$field]['empty_column']) ? $this->options['info'][$field]['empty_column'] : FALSE,
      '#states' => array(
        'visible' => array(
          $column_selector => array(
            'value' => $field,
          ),
        ),
      ),
    );
    $form['info'][$field]['responsive'] = array(
      '#type' => 'select',
      '#default_value' => isset($this->options['info'][$field]['responsive']) ? $this->options['info'][$field]['responsive'] : '',
      '#options' => array(
        '' => t('None'),
        RESPONSIVE_PRIORITY_MEDIUM => t('Medium'),
        RESPONSIVE_PRIORITY_LOW => t('Low'),
      ),
      '#states' => array(
        'visible' => array(
          $column_selector => array(
            'value' => $field,
          ),
        ),
      ),
    );

    // markup for the field name
    $form['info'][$field]['name'] = array(
      '#markup' => $field_names[$field],
    );
  }

  // Provide a radio for no default sort
  $form['default'][-1] = array(
    '#type' => 'radio',
    '#return_value' => -1,
    '#parents' => array(
      'style_options',
      'default',
    ),
    '#id' => 'edit-default-0',
    '#default_value' => $default,
  );
  $form['empty_table'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show the empty text in the table'),
    '#default_value' => $this->options['empty_table'],
    '#description' => t('Per default the table is hidden for an empty view. With this option it is posible to show an empty table with the text in it.'),
  );
  $form['description_markup'] = array(
    '#markup' => '<div class="description form-item">' . t('Place fields into columns; you may combine multiple fields into the same column. If you do, the separator in the column specified will be used to separate the fields. Check the sortable box to make that column click sortable, and check the default sort radio to determine which column will be sorted by default, if any. You may control column order and field labels in the fields section.') . '</div>',
  );
}