You are here

public function SmartDateProcessor::process in Smart Date 3.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/FullcalendarViewProcessor/SmartDateProcessor.php \Drupal\smart_date\Plugin\FullcalendarViewProcessor\SmartDateProcessor::process()
  2. 3.0.x src/Plugin/FullcalendarViewProcessor/SmartDateProcessor.php \Drupal\smart_date\Plugin\FullcalendarViewProcessor\SmartDateProcessor::process()
  3. 3.1.x src/Plugin/FullcalendarViewProcessor/SmartDateProcessor.php \Drupal\smart_date\Plugin\FullcalendarViewProcessor\SmartDateProcessor::process()
  4. 3.2.x src/Plugin/FullcalendarViewProcessor/SmartDateProcessor.php \Drupal\smart_date\Plugin\FullcalendarViewProcessor\SmartDateProcessor::process()
  5. 3.3.x src/Plugin/FullcalendarViewProcessor/SmartDateProcessor.php \Drupal\smart_date\Plugin\FullcalendarViewProcessor\SmartDateProcessor::process()
  6. 3.4.x src/Plugin/FullcalendarViewProcessor/SmartDateProcessor.php \Drupal\smart_date\Plugin\FullcalendarViewProcessor\SmartDateProcessor::process()

@inheritDoc

processing view results of fullcalendar_view for a smart date field

ToDo:

  • handling of drag and drop updates full calendar
  • timezone handling
  • smart date recurring events handling (should be roughly working)
  • fullcalender_view recurring events handling (not considered yet, maybe

this is not to be supported by smart date anyway

File

src/Plugin/FullcalendarViewProcessor/SmartDateProcessor.php, line 33

Class

SmartDateProcessor
Smart Date plugin.

Namespace

Drupal\smart_date\Plugin\FullcalendarViewProcessor

Code

public function process(array &$variables) {

  /* @var \Drupal\views\ViewExecutable $view */
  $view = $variables['view'];
  $view_index = key($variables['#attached']['drupalSettings']['fullCalendarView']);
  $fields = $view->field;
  $options = $view->style_plugin->options;
  $start_field = $options['start'];
  $start_field_options = $fields[$start_field]->options;
  $format_label = $start_field_options['settings']['format'];

  // Load the format specified in the View.
  $format = SmartDateTrait::loadSmartDateFormat($format_label);
  $multiple = $fields[$start_field]->multiple;

  // If not a Smart Date field or not existing config, nothing to do.
  if (strpos($start_field_options['type'], 'smartdate') !== 0 || empty($variables['#attached']['drupalSettings']['fullCalendarView'][$view_index]['calendar_options'])) {
    return;
  }
  $calendar_options = json_decode($variables['#attached']['drupalSettings']['fullCalendarView'][$view_index]['calendar_options'], TRUE);
  $entries = $calendar_options['events'];
  $mappings = $this
    ->getIdMappings($entries);
  if ($multiple && $start_field_options['group_rows'] == FALSE) {
    $messenger = \Drupal::messenger();
    $messenger
      ->addMessage('Please group the rows', $messenger::TYPE_WARNING);
  }
  foreach ($view->result as $key => $row) {
    $current_entity = $row->_entity;
    $values = $current_entity
      ->get($start_field)
      ->getValue();
    $row_data = [];
    $row_data['format'] = $format;
    if ($multiple) {
      foreach ($values as $delta => $value) {
        $value['delta'] = $delta;
        $value['id'] = $row->_entity
          ->id();
        $lookup_key = $value['id'] . '-' . $delta;
        $entries_index = $mappings[$lookup_key];
        $this
          ->updateEntry($entries[$entries_index], $value, $format);
      }
    }
    else {
      $values[0]['delta'] = $key;
      $values[0]['id'] = $row->_entity
        ->id();
      $this
        ->updateEntry($entries[$key], $values[0], $format);
    }
  }

  // Update the entries.
  if ($entries) {
    $calendar_options['events'] = $entries;
    $variables['#attached']['drupalSettings']['fullCalendarView'][$view_index]['calendar_options'] = json_encode($calendar_options);
  }
}