You are here

public function EntityRow::buildOptionsForm in Views Parity Row 8

Provide a form for setting options.

Overrides EntityRow::buildOptionsForm

File

src/Plugin/views/row/EntityRow.php, line 31

Class

EntityRow
Generic entity row plugin to provide a common base for all entity types.

Namespace

Drupal\views_parity_row\Plugin\views\row

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  $options = $this->options['views_parity_row'];
  $default = $this
    ->defineOptions();
  $form['views_parity_row_enable'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Alternate with a different view mode every X rows ?'),
    '#default_value' => isset($this->options['views_parity_row_enable']) ? $this->options['views_parity_row_enable'] : $default['views_parity_row_enable'],
  ];
  $form['views_parity_row'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Configuration of Views Parity Row'),
    '#states' => [
      'visible' => [
        ':input[name="row_options[views_parity_row_enable]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
    'frequency' => [
      '#type' => 'number',
      '#title' => $this
        ->t('Frequency of view mode change.'),
      '#description' => $this
        ->t('Enter a positive integer number. This number will be the frequency of change of the content view mode. Example, if you chose <em>3</em>, it means that every 3 rows, the content will use the other View mode.'),
      '#size' => 6,
      '#maxlength' => 6,
      '#default_value' => isset($options['frequency']) ? (int) $options['frequency'] : $default['views_parity_row']['frequency']['default'],
    ],
    'start' => [
      '#type' => 'number',
      '#title' => $this
        ->t('Start'),
      '#description' => $this
        ->t('Start at which row ?'),
      '#min' => 0,
      '#size' => 6,
      '#maxlength' => 6,
      '#default_value' => isset($options['start']) ? (int) $options['start'] : $default['views_parity_row']['start']['default'],
    ],
    'end' => [
      '#type' => 'number',
      '#title' => $this
        ->t('End'),
      '#description' => $this
        ->t('End at which row ? Set <em>0</em> to disable.'),
      '#min' => 0,
      '#size' => 6,
      '#maxlength' => 6,
      '#default_value' => isset($options['end']) ? (int) $options['end'] : $default['views_parity_row']['end']['default'],
    ],
    'view_mode' => [
      '#type' => 'select',
      '#options' => $this->entityDisplayRepository
        ->getViewModeOptions($this->entityTypeId),
      '#title' => $this
        ->t('Alternate view mode'),
      '#default_value' => isset($options['view_mode']) ? $options['view_mode'] : $default['views_parity_row']['view_mode']['default'],
    ],
  ];
  $form['views_parity_row_per_row_enable'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Alternate with a different view mode per row'),
    '#default_value' => isset($this->options['views_parity_row_per_row_enable']) ? $this->options['views_parity_row_per_row_enable'] : $default['views_parity_row_per_row_enable'],
  ];
  $form['views_parity_row_per_row'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Configuration of Views Parity Row Per Row'),
    '#states' => [
      'visible' => [
        ':input[name="row_options[views_parity_row_per_row_enable]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $options = $this->options['views_parity_row_per_row'];
  for ($i = 1; $i <= 20; $i++) {
    $form['views_parity_row_per_row']['view_mode_' . $i] = [
      '#type' => 'select',
      '#options' => [
        '' => $this
          ->t('- No alternate view mode -'),
      ] + $this->entityDisplayRepository
        ->getViewModeOptions($this->entityTypeId),
      '#title' => $this
        ->t('Alternate view mode for row @row', [
        '@row' => $i,
      ]),
      '#default_value' => isset($options['view_mode_' . $i]) ? $options['view_mode_' . $i] : '',
    ];
  }
}