You are here

content_calendar.module in Content Planner 8

File

modules/content_calendar/content_calendar.module
View source
<?php

/**
 * @file
 * Contains content_calendar.module.
 */
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\content_calendar\DateTimeHelper;
use Drupal\Core\Datetime\DrupalDateTime;

/**
 * Implements hook_theme().
 */
function content_calendar_theme($existing, $type, $theme, $path) {
  return [
    'content_calendar_overview' => [
      'variables' => [
        'calendars' => [],
        'filters_form' => [],
        'has_permission' => FALSE,
      ],
    ],
    'content_calendar' => array(
      'variables' => array(
        'calendar' => array(),
        'node_type_creation_permissions' => array(),
      ),
    ),
    'content_calendar_entry' => [
      'variables' => [
        'node' => NULL,
        'node_type_config' => NULL,
        'calendar_id' => NULL,
        'month' => NULL,
        'year' => NULL,
        'user_picture' => NULL,
        'options' => [],
        'workflow_state' => NULL,
      ],
    ],
    'content_calendar_legend' => [
      'variables' => [
        'content_type_configs' => [],
      ],
    ],
    'content_calendar_jump_links' => [
      'variables' => [
        'months' => [],
        'year' => NULL,
      ],
    ],
    'recent_calendar_content' => [
      'variables' => [
        'last_nodes' => [],
        'next_nodes' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function content_calendar_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // If the user is on a form to add a new node.
  if (\Drupal::routeMatch()
    ->getRouteName() == 'node.add') {

    // Get Node Type from Route.
    $node_type = \Drupal::routeMatch()
      ->getParameter('node_type');

    /**
     * @var $content_type_config_service \Drupal\content_calendar\ContentTypeConfigService
     */
    $content_type_config_service = \Drupal::service('content_calendar.content_type_config_service');

    // If there is a creation date in the query string.
    if ($content_type_config_service
      ->loadEntityByContentType($node_type
      ->id()) && \Drupal::request()->query
      ->has('publish_on')) {

      // Get date from query string.
      $date = \Drupal::request()->query
        ->get('publish_on');

      // If the date is a valid MySQL Date.
      if (DateTimeHelper::dateIsMySQLDateOnly($date)) {

        // Check scheduler for default time
        $scheduler_config = \Drupal::config('scheduler.settings');
        $default_time = FALSE;
        if ($scheduler_config
          ->get('allow_date_only')) {
          $default_time = $scheduler_config
            ->get('default_time');
        }

        // Create DrupalDateTime object.
        if ($default_time) {
          $datetime = DrupalDateTime::createFromFormat('Y-m-d H:i:s', $date . ' ' . $default_time);
        }
        else {
          $datetime = DrupalDateTime::createFromFormat('Y-m-d', $date);
        }

        // Assign date to the created field.
        $form['created']['widget'][0]['value']['#default_value'] = $datetime;

        // Assign date to the scheduler date, if it exists.
        if (\Drupal::currentUser()
          ->hasPermission('schedule publishing of nodes')) {
          if (array_key_exists('publish_on', $form)) {
            $form['publish_on']['widget'][0]['value']['#default_value'] = $datetime;
          }
        }
      }
    }
  }
}

/**
 * Implements hook_toolbar_alter().
 */
function content_calendar_toolbar_alter(&$items) {
  $links =& $items['content_planner']['tray']['links']['#items'];
  if (\Drupal::currentUser()
    ->hasPermission('manage content calendar') || \Drupal::currentUser()
    ->hasPermission('view content calendar') || \Drupal::currentUser()
    ->hasPermission('administer content calendar settings')) {
    $links['content_calendar'] = [
      '#type' => 'link',
      '#title' => t('Content Calendar'),
      '#url' => Url::fromRoute('content_calendar.current'),
      '#attributes' => [
        'class' => 'toolbar-icon toolbar-icon-system-admin-content',
      ],
    ];
  }
}