You are here

public function Calendar::buildOptionsForm in Calendar 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/views/style/Calendar.php \Drupal\calendar\Plugin\views\style\Calendar::buildOptionsForm()

Provide a form to edit options for this plugin.

Overrides StylePluginBase::buildOptionsForm

File

src/Plugin/views/style/Calendar.php, line 232

Class

Calendar
Views style plugin for the Calendar module.

Namespace

Drupal\calendar\Plugin\views\style

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  $form['granularity'] = [
    '#title' => $this
      ->t('Calendar granularity'),
    '#type' => 'select',
    '#description' => $this
      ->t('Select the time period for this display.'),
    '#default_value' => $this->options['granularity'],
    '#options' => [
      'day' => $this
        ->t('Day'),
      'week' => $this
        ->t('Week'),
      'month' => $this
        ->t('Month'),
      'year' => $this
        ->t('Year'),
    ],
  ];
  $form['day_name_size'] = [
    '#title' => $this
      ->t('Calendar day of week names'),
    '#default_value' => $this->options['day_name_size'],
    '#type' => 'radios',
    '#options' => [
      1 => $this
        ->t('First letter of name'),
      2 => $this
        ->t('First two letters of name'),
      3 => $this
        ->t('Abbreviated name'),
      99 => $this
        ->t('Full name'),
    ],
    '#description' => $this
      ->t('The way day of week names should be displayed in a calendar.'),
    '#states' => [
      'visible' => [
        ':input[name="style_options[granularity]"]' => [
          [
            'value' => 'year',
          ],
          [
            'value' => 'month',
          ],
          [
            'value' => 'week',
          ],
        ],
      ],
    ],
  ];
  $form['mini'] = [
    '#title' => $this
      ->t('Display as mini calendar'),
    '#default_value' => $this->options['mini'],
    '#type' => 'radios',
    '#options' => [
      0 => $this
        ->t('No'),
      1 => $this
        ->t('Yes'),
    ],
    '#description' => $this
      ->t('Display the mini style calendar, with no item details. Suitable for a calendar displayed in a block.'),
    '#dependency' => [
      'edit-style-options-granularity' => [
        'month',
      ],
    ],
    '#states' => [
      'visible' => [
        ':input[name="style_options[granularity]"]' => [
          'value' => 'month',
        ],
      ],
    ],
  ];
  $form['with_weekno'] = [
    '#title' => $this
      ->t('Show week numbers'),
    '#default_value' => $this->options['with_weekno'],
    '#type' => 'radios',
    '#options' => [
      0 => $this
        ->t('No'),
      1 => $this
        ->t('Yes'),
    ],
    '#description' => $this
      ->t('Whether or not to show week numbers in the calendar.'),
  ];

  // Get views with Calendar arguments to display as link.
  $calendar_displays = $this->calendarHelper
    ->getCalendarDisplays();
  $form['granularity_links'] = [
    '#tree' => TRUE,
  ];
  $options_week = [
    $calendar_displays['week']['route'] => $calendar_displays['week']['title'],
  ];
  $options_day = [
    $calendar_displays['day']['route'] => $calendar_displays['day']['title'],
  ];
  $form['granularity_links']['week'] = [
    '#title' => $this
      ->t('Week link displays'),
    '#type' => 'select',
    '#default_value' => $this->options['granularity_links']['week'],
    '#description' => $this
      ->t("Optionally select a View display to use for Week links."),
    '#options' => [
      '' => $this
        ->t('Select...'),
    ] + $options_week,
  ];
  $form['granularity_links']['day'] = [
    '#title' => $this
      ->t('Day link displays'),
    '#type' => 'select',
    '#default_value' => $this->options['granularity_links']['day'],
    '#description' => $this
      ->t("Optionally select a View display to use for Day links."),
    '#options' => [
      '' => $this
        ->t('Select...'),
    ] + $options_day,
  ];
  $form['multi_allday_style'] = [
    '#title' => $this
      ->t('Multi- & allday style'),
    '#default_value' => $this->options['multi_allday_style'],
    '#type' => 'select',
    '#options' => [
      0 => $this
        ->t('Display multi- & allday item as title'),
      1 => $this
        ->t('Display multi- & allday item as view mode'),
    ],
    '#description' => $this
      ->t('Select how to show multi- & allday items.'),
    '#states' => [
      'visible' => [
        ':input[name="style_options[granularity]"]' => [
          [
            'value' => 'month',
          ],
          [
            'value' => 'week',
          ],
          [
            'value' => 'day',
          ],
        ],
      ],
    ],
  ];
  $form['multi_allday_title'] = [
    '#title' => $this
      ->t('Multi- & allday title style'),
    '#default_value' => $this->options['multi_allday_title'],
    '#type' => 'select',
    '#options' => [
      0 => $this
        ->t('Display title as plain text'),
      1 => $this
        ->t('Display title as link to entity'),
    ],
    '#description' => $this
      ->t('Select how titles are shown.'),
    '#states' => [
      'visible' => [
        ':input[name="style_options[multi_allday_style]"]' => [
          [
            'value' => 0,
          ],
        ],
      ],
    ],
  ];

  // Get relevant viewmodes.
  $view_modes = \Drupal::service('entity_display.repository')
    ->getViewModes($this
    ->getBaseEntityType());
  foreach ($view_modes as $mode => $settings) {
    $options[$mode] = $settings['label'];
  }
  $form['multi_allday_viewmode'] = [
    '#title' => $this
      ->t('Multi- & allday viewmode'),
    '#default_value' => $this->options['multi_allday_title'],
    '#type' => 'select',
    '#options' => $options,
    '#description' => $this
      ->t('Select viewmode for multi- & allday.'),
    '#states' => [
      'visible' => [
        ':input[name="style_options[multi_allday_style]"]' => [
          [
            'value' => 1,
          ],
        ],
      ],
    ],
  ];

  // Use any available field from color_field module.
  $moduleHandler = \Drupal::service('module_handler');
  if ($moduleHandler
    ->moduleExists('color_field')) {
    $options = [];
    $dependencies = $this->view->storage
      ->getDependencies();
    $config = $dependencies['config'];
    foreach ($config as $detail) {
      if (substr($detail, 0, strlen('field_storage')) === 'field.storage') {
        $field = \Drupal::entityTypeManager()
          ->getStorage('field_storage_config')
          ->load(substr($detail, 14));
        if ($field
          ->getType() === 'color_field_type') {
          $options[$field
            ->id()] = $field
            ->getName() . ' (' . $field
            ->id() . ')';
        }
      }
    }
    array_unshift($options, 'None.');
    $form['colors'] = [
      '#title' => $this
        ->t('Use a field for event colors'),
      '#default_value' => $this->options['colors'],
      '#type' => 'select',
      '#options' => $options,
    ];
  }
}