You are here

public function Calendar::buildOptionsForm in Calendar 8

Same name in this branch
  1. 8 src/Plugin/views/style/Calendar.php \Drupal\calendar\Plugin\views\style\Calendar::buildOptionsForm()
  2. 8 src/Plugin/views/row/Calendar.php \Drupal\calendar\Plugin\views\row\Calendar::buildOptionsForm()

Provide a form for setting options.

Overrides RowPluginBase::buildOptionsForm

File

src/Plugin/views/row/Calendar.php, line 153

Class

Calendar
Plugin which creates a view on the resulting object and formats it as a Calendar entity.

Namespace

Drupal\calendar\Plugin\views\row

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  $form['markup'] = [
    '#markup' => $this
      ->t("The calendar row plugin will format view results as calendar items. Make sure this display has a 'Calendar' format and uses a 'Date' contextual filter, or this plugin will not work correctly."),
  ];
  $form['colors'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Legend Colors'),
    '#description' => $this
      ->t('Set a hex color value (like #ffffff) to use in the calendar legend for each content type. Items with empty values will have no stripe in the calendar and will not be added to the legend.'),
  ];
  $options = [];

  // @todo Allow strip options for any bundes of any entity type
  if ($this->view
    ->getBaseTables()['node_field_data']) {
    $options['type'] = $this
      ->t('Based on Content Type');
  }
  if (\Drupal::moduleHandler()
    ->moduleExists('taxonomy')) {
    $options['taxonomy'] = $this
      ->t('Based on Taxonomy');
  }

  // If no option is available, stop here.
  if (empty($options)) {
    return;
  }
  $form['colors']['legend'] = [
    '#title' => $this
      ->t('Stripes'),
    '#description' => $this
      ->t('Add stripes to calendar items.'),
    '#type' => 'select',
    '#options' => $options,
    '#empty_value' => (string) $this
      ->t('None'),
    '#default_value' => $this->options['colors']['legend'],
  ];
  if ($this->view
    ->getBaseTables()['node_field_data']) {
    $colors = $this->options['colors']['calendar_colors_type'];
    $type_names = node_type_get_names();
    foreach ($type_names as $key => $name) {
      $form['colors']['calendar_colors_type'][$key] = [
        '#title' => $name,
        '#default_value' => isset($colors[$key]) ? $colors[$key] : CALENDAR_EMPTY_STRIPE,
        '#dependency' => [
          'edit-row-options-colors-legend' => [
            'type',
          ],
        ],
        '#type' => 'textfield',
        '#size' => 7,
        '#maxlength' => 7,
        '#element_validate' => [
          [
            $this,
            'validateHexColor',
          ],
        ],
        '#prefix' => '<div class="calendar-colorpicker-wrapper">',
        '#suffix' => '<div class="calendar-colorpicker"></div></div>',
        '#attributes' => [
          'class' => [
            'edit-calendar-colorpicker',
          ],
        ],
        '#attached' => [
          // Add Farbtastic color picker and the js to trigger it.
          'library' => [
            'calendar/calendar.colorpicker',
          ],
        ],
      ] + $this
        ->visibleOnLegendState('type');
    }
  }
  if (\Drupal::moduleHandler()
    ->moduleExists('taxonomy')) {

    // Get the display's field names of taxonomy fields.
    $vocabulary_field_options = [];
    $fields = $this->displayHandler
      ->getOption('fields');
    foreach ($fields as $name => $field_info) {

      // Select the proper field type.
      if ($this
        ->isTermReferenceField($field_info, $this->fieldManager)) {
        $vocabulary_field_options[$name] = $field_info['label'] ?: $name;
      }
    }
    if (empty($vocabulary_field_options)) {
      return;
    }
    $form['colors']['taxonomy_field'] = [
      '#title' => $this
        ->t('Term field'),
      '#type' => !empty($vocabulary_field_options) ? 'select' : 'hidden',
      '#default_value' => $this->options['colors']['taxonomy_field'],
      '#empty_value' => (string) $this
        ->t('None'),
      '#description' => $this
        ->t("Select the taxonomy term field to use when setting stripe colors. This works best for vocabularies with only a limited number of possible terms."),
      '#options' => $vocabulary_field_options,
      // @todo Is this in the form api?
      '#dependency' => [
        'edit-row-options-colors-legend' => [
          'taxonomy',
        ],
      ],
    ] + $this
      ->visibleOnLegendState('taxonomy');
    if (empty($vocabulary_field_options)) {
      $form['colors']['taxonomy_field']['#options'] = [
        '' => '',
      ];
      $form['colors']['taxonomy_field']['#suffix'] = $this
        ->t('You must add a term field to this view to use taxonomy stripe values. This works best for vocabularies with only a limited number of possible terms.');
    }

    // Get the Vocabulary names.
    $vocab_vids = [];
    foreach ($vocabulary_field_options as $field_name => $label) {

      // @todo Provide storage manager via Dependency Injection
      $field_config = \Drupal::entityTypeManager()
        ->getStorage('field_config')
        ->loadByProperties([
        'field_name' => $field_name,
      ]);

      // @todo refactor
      reset($field_config);
      $key = key($field_config);
      $data = \Drupal::config('field.field.' . $field_config[$key]
        ->getOriginalId())
        ->getRawData();
      if ($target_bundles = $data['settings']['handler_settings']['target_bundles']) {

        // Fields must target bundles set.
        reset($target_bundles);
        $vocab_vids[$field_name] = key($target_bundles);
      }
    }
    if (empty($vocab_vids)) {
      return;
    }
    $this->options['colors']['calendar_colors_vocabulary'] = $vocab_vids;
    $form['colors']['calendar_colors_vocabulary'] = [
      '#title' => $this
        ->t('Vocabulary Legend Types'),
      '#type' => 'value',
      '#value' => $vocab_vids,
    ] + $this
      ->visibleOnLegendState('taxonomy');

    // Get the Vocabulary term id's and map to colors.
    // @todo Add labels for each Vocabulary.
    $term_colors = $this->options['colors']['calendar_colors_taxonomy'];
    foreach ($vocab_vids as $field_name => $vid) {
      $vocab = \Drupal::entityTypeManager()
        ->getStorage("taxonomy_term")
        ->loadTree($vid);
      foreach ($vocab as $key => $term) {
        $form['colors']['calendar_colors_taxonomy'][$term->tid] = [
          '#title' => $this
            ->t('@term_name', [
            '@term_name' => $term->name,
          ]),
          '#default_value' => isset($term_colors[$term->tid]) ? $term_colors[$term->tid] : CALENDAR_EMPTY_STRIPE,
          '#access' => !empty($vocabulary_field_options),
          '#dependency_count' => 2,
          '#dependency' => [
            'edit-row-options-colors-legend' => [
              'taxonomy',
            ],
            'edit-row-options-colors-taxonomy-field' => [
              $field_name,
            ],
          ],
          '#type' => 'textfield',
          '#size' => 7,
          '#maxlength' => 7,
          '#element_validate' => [
            [
              $this,
              'validateHexColor',
            ],
          ],
          '#prefix' => '<div class="calendar-colorpicker-wrapper">',
          '#suffix' => '<div class="calendar-colorpicker"></div></div>',
          '#attributes' => [
            'class' => [
              'edit-calendar-colorpicker',
            ],
          ],
          '#attached' => [
            // Add Farbtastic color picker and the js to trigger it.
            'library' => [
              'calendar/calendar.colorpicker',
            ],
          ],
        ] + $this
          ->visibleOnLegendState('taxonomy');
      }
    }
  }
}