You are here

social_event_type.module in Open Social 8.4

The Social event type module.

File

modules/social_features/social_event/modules/social_event_type/social_event_type.module
View source
<?php

/**
 * @file
 * The Social event type module.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;

/**
 * Implements hook_form_FORM_ID_alter().
 */
function social_event_type_form_node_event_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  social_event_type_widget_alter($form);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function social_event_type_form_node_event_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  social_event_type_widget_alter($form);
}

/**
 * Function that does some magic to the event type field.
 *
 * @param array $form
 *   Form array.
 */
function social_event_type_widget_alter(array &$form) {

  /** @var \Drupal\Core\Config\ImmutableConfig $config */
  $config = \Drupal::config('social_event_type.settings');

  // Check if the field is set to mandatory.
  if ($config
    ->get('social_event_type_required') == TRUE) {
    $form['field_event_type']['widget']['#required'] = TRUE;
  }

  // Set the event type label for none.
  if (isset($form['field_event_type']['widget']['#options']['_none'])) {

    // If the field was set to mandatory, remove the _none option.
    if ($config
      ->get('social_event_type_required') == TRUE) {
      unset($form['field_event_type']['widget']['#options']['_none']);
    }
  }

  // Change the widget if more than X.
  if (count($form['field_event_type']['widget']['#options']) >= $config
    ->get('social_event_type_select_changer')) {
    $form['field_event_type']['widget']['#type'] = 'select';
  }
}

/**
 * Preprocess event node.
 *
 * @param array $variables
 *   Array with variables of a node.
 */
function social_event_type_preprocess_node(array &$variables) {

  /* @var \Drupal\node\NodeInterface $node */
  $node = $variables['node'];
  if ($node
    ->getType() === 'event') {
    $event_type = $node
      ->get('field_event_type');
    $event_type_entities = $event_type
      ->referencedEntities();
    if (count($event_type_entities) === 1) {

      /** @var \Drupal\node\Entity\Node $event */
      foreach ($event_type_entities as $event) {
        $variables['metadata'] = t('in @event', [
          '@event' => $event
            ->link(),
        ]);

        // Set event type link.
        $now = strtotime("now");
        $end = strtotime($node
          ->get('field_event_date_end')
          ->getString());

        // 1 are upcoming events.
        // 2 are ones that have started or are finished.
        $datetype = $now > $end ? 2 : 1;
        $event_type_url = Url::fromRoute('view.upcoming_events.page_community_events', [
          'field_event_date_value' => $datetype,
          'event_type_id' => $event
            ->id(),
        ]);
        $event_type_link = Link::fromTextAndUrl($event
          ->label(), $event_type_url)
          ->toString();
        $variables['event_type'] = $event_type_link;
      }
    }
    else {
      $variables['metadata'] = NULL;
      $variables['event_type'] = NULL;
    }
  }
}