You are here

bat_event.install in Booking and Availability Management Tools for Drupal 8

Same filename and directory in other branches
  1. 7 modules/bat_event/bat_event.install

Sets up the base table for our entity and a table to store information about the entity types.

File

modules/bat_event/bat_event.install
View source
<?php

/**
 * @file
 * Sets up the base table for our entity and a table to store information about
 * the entity types.
 */
use Drupal\Core\Database\Database;
use Drupal\field\Entity\FieldConfig;

/**
 * Implements hook_requirements().
 */
function bat_event_requirements($phase) {
  if ($phase != 'runtime') {
    return [];
  }
  if (!class_exists('Roomify\\Bat\\Unit\\Unit')) {
    return [
      'roomify' => [
        'title' => t('BAT'),
        'value' => t('Roomify BAT Library Missing'),
        'description' => t('The Roomify BAT Library is missing from your site. Please try re-running the composer update command.'),
        'severity' => REQUIREMENT_ERROR,
      ],
    ];
  }
  else {
    return [
      'roomify' => [
        'title' => t('BAT'),
        'value' => t('The Roomify BAT Library is installed.'),
        'severity' => REQUIREMENT_OK,
      ],
    ];
  }
}

/**
 * Migrate event's dates on the new field.
 */
function bat_event_update_8001() {
  $events = \Drupal::database()
    ->select('event', 'e')
    ->fields('e', [])
    ->condition('start', '', '<>')
    ->execute()
    ->fetchAll();
  foreach ($events as $event) {
    $event_object = bat_event_load($event->id);
    $start_date = new DateTime();
    $start_date
      ->setTimestamp($event->start);
    $end_date = new DateTime();
    $end_date
      ->setTimestamp($event->end);
    $event_object
      ->setStartDate($start_date);
    $event_object
      ->setEndDate($end_date);
    $event_object
      ->save();
  }
}

/**
 * Drop old "start" and "end" columns on event table.
 */
function bat_event_update_8002() {
  Database::getConnection()
    ->schema()
    ->dropField('event', 'start');
  Database::getConnection()
    ->schema()
    ->dropField('event', 'end');
}

/**
 * Set Event fields as required.
 */
function bat_event_update_8003() {
  foreach (bat_event_get_types() as $event_type) {
    $field_names = [
      'event_dates',
      'event_' . $event_type
        ->getTargetEntityType() . '_reference',
    ];
    if ($event_type
      ->getFixedEventStates()) {
      $field_names[] = 'event_state_reference';
    }
    foreach ($field_names as $field_name) {
      $field = FieldConfig::loadByName('bat_event', $event_type
        ->id(), $field_name);
      $field
        ->setRequired(TRUE);
      $field
        ->save();
    }
  }
}

Functions

Namesort descending Description
bat_event_requirements Implements hook_requirements().
bat_event_update_8001 Migrate event's dates on the new field.
bat_event_update_8002 Drop old "start" and "end" columns on event table.
bat_event_update_8003 Set Event fields as required.