content_planner-8.x-1.x-dev/modules/content_calendar/content_calendar.module

modules/content_calendar/content_calendar.module
<?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' => [
      'variables' => [
        'calendar' => [],
        'weekdays' => [],
        'node_type_creation_permissions' => [],
        'add_content_set_schedule_date' => NULL,
      ],
    ],

    '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');

    /**
     * Drupal\content_calendar\ContentTypeConfigService definition.
     *
     * @var \Drupal\content_calendar\ContentTypeConfigService $content_type_config_service
     */
    $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('created')) {

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

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

        // Check scheduler's configuration.
        $scheduler_config = \Drupal::config('scheduler.settings');
        $datetime_format = "Y-m-d";

        if ($scheduler_config->get('allow_date_only')) {
          $date .= ' ' . $scheduler_config->get('default_time');
          $datetime_format = "Y-m-d H:i:s";

          if ($scheduler_config->get('hide_seconds')) {
            $datetime_format = "Y-m-d H:i";
          }
        }

        // Create DrupalDateTime object.
        $datetime = DrupalDateTime::createFromFormat($datetime_format, $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')
          && array_key_exists('publish_on', $form)
          && \Drupal::request()->query->get('schedule')
        ) {
          $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',
      ],
    ];
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc