class CalendarController in Content Planner 8
Class CalendarController.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\content_calendar\Controller\CalendarController
Expanded class hierarchy of CalendarController
File
- modules/
content_calendar/ src/ Controller/ CalendarController.php, line 20
Namespace
Drupal\content_calendar\ControllerView source
class CalendarController extends ControllerBase {
/**
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* Drupal\content_calendar\ContentTypeConfigService definition.
*
* @var \Drupal\content_calendar\ContentTypeConfigService
*/
protected $contentTypeConfigService;
/**
* @var \Drupal\content_calendar\ContentCalendarService
*/
protected $contentCalendarService;
/**
* Constructs a new CalendarController object.
*/
public function __construct(RequestStack $request_stack, ContentTypeConfigService $content_type_config_service, ContentCalendarService $content_calendar_service, AccountProxyInterface $current_user) {
$this->request = $request_stack
->getCurrentRequest();
$this->contentTypeConfigService = $content_type_config_service;
$this->contentCalendarService = $content_calendar_service;
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('request_stack'), $container
->get('content_calendar.content_type_config_service'), $container
->get('content_calendar.content_calendar_service'), $container
->get('current_user'));
}
/**
* Show Calendar year.
*/
public function showCurrentCalendarYear() {
$year = date('Y');
return $this
->showCalendarYear($year);
}
/**
* Show Calendar year.
*/
public function showCalendarYear($year) {
$calendars = [];
// Get content type config entities.
$content_type_config_entities = $this->contentTypeConfigService
->loadAllEntities();
// Check if Content Calendar has been configured.
if (!$content_type_config_entities) {
$this
->messenger()
->addMessage($this
->t('Content Calendar is not configured yet. Please do this in the settings tab.'), 'error');
return [];
}
// Generate calendar structures.
foreach (range(1, 12) as $month) {
// Create new Calendar.
$calender = new Calendar($this->contentTypeConfigService, $this->contentCalendarService, $month, $year, $this->currentUser);
$calendars[] = $calender
->build();
}
// Get Filter Form.
$form_params = [
'current_year' => date('Y'),
'selected_year' => $year,
];
$filters_form = \Drupal::formBuilder()
->getForm('Drupal\\content_calendar\\Form\\CalenderOverviewFilterForm', $form_params);
if (\Drupal::currentUser()
->hasPermission('administer content calendar settings')) {
$has_permission = TRUE;
}
else {
$has_permission = FALSE;
}
$build = [
'#theme' => 'content_calendar_overview',
'#calendars' => $calendars,
'#filters_form' => $filters_form,
'#has_permission' => $has_permission,
];
return $build;
}
/**
* Update creation date of a given Node.
*
* @param \Drupal\node\NodeInterface $node
* @param string $date
*
* @return \Zend\Diactoros\Response\JsonResponse
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function updateNodePublishDate(NodeInterface $node, $date) {
$data = [
'success' => FALSE,
'message' => NULL,
];
// Get content type config entities.
$content_type_config_entities = $this->contentTypeConfigService
->loadAllEntities();
// Check for allowed types, marked in the Content Calendar settings.
if (!array_key_exists($node
->getType(), $content_type_config_entities)) {
$data['message'] = $this
->t('Action is not allowed for Nodes of type @type', [
'@type' => $node
->getType(),
]);
return new JsonResponse($data);
}
// First – Update created on date!
// Get the Node's "created on" date
$created_on_timestamp = $node
->get('created')
->getValue();
$created_on_timestamp_value = $created_on_timestamp[0]['value'];
// Return a date object
$original_created_on_datetime = DateTimeHelper::convertUnixTimestampToDatetime($created_on_timestamp_value);
// Extract hour, minutes and seconds.
$hour = $original_created_on_datetime
->format('H');
$minutes = $original_created_on_datetime
->format('i');
$seconds = $original_created_on_datetime
->format('s');
// Create a new datetime object from the given date.
$new_created_on_datetime = \DateTime::createFromFormat('Y-m-d', $date);
// Set hour, minutes and seconds.
$new_created_on_datetime
->setTime($hour, $minutes, $seconds);
// Set created time.
$node
->set('created', $new_created_on_datetime
->getTimestamp());
// Second - Update publish on date! (only if publish on date is set)
// Get publish on timestamp.
$publish_on_timestamp = $node
->get('publish_on')
->getValue();
$publish_on_timestamp_value = $publish_on_timestamp[0]['value'];
// Only change scheduler publish on timestamp, when "publish on" is set
if ($publish_on_timestamp_value) {
// Get the Node's publish ondate and return a datetime object.
$original_publish_datetime = DateTimeHelper::convertUnixTimestampToDatetime($publish_on_timestamp_value);
// Extract hour, minutes and seconds.
$hour = $original_publish_datetime
->format('H');
$minutes = $original_publish_datetime
->format('i');
$seconds = $original_publish_datetime
->format('s');
// Create a new datetime object from the given date.
$new_publish_datetime = \DateTime::createFromFormat('Y-m-d', $date);
// Set hour, minutes and seconds.
$new_publish_datetime
->setTime($hour, $minutes, $seconds);
// Set publish on datetime.
$node
->set('publish_on', $new_publish_datetime
->getTimestamp());
// Set created on datetime.
$node
->set('created', $new_publish_datetime
->getTimestamp());
}
// Save.
if ($node
->save() == SAVED_UPDATED) {
$data['success'] = TRUE;
$data['message'] = $this
->t('The creation date for Node @id has been updated', [
'@id' => $node
->id(),
]);
}
return new JsonResponse($data);
}
/**
* Redirect to current Calendar.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function redirectToCurrentCalendar() {
$calendar_id = date('Y-n');
return $this
->redirect('content_calendar.calendar', [], [
'fragment' => $calendar_id,
]);
}
/**
* Redirect and jump to a given Calendar directly.
*
* @param string $calendar_id
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function redirectToCalendar($year, $month) {
$fragment = $year . '-' . $month;
return $this
->redirect('content_calendar.calendar', [
'year' => $year,
], [
'fragment' => $fragment,
]);
}
/**
* Duplicate Node.
*
* @param \Drupal\node\NodeInterface $node
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function duplicateNode(NodeInterface $node) {
$duplicate = $node
->createDuplicate();
$duplicate
->setTitle($duplicate
->getTitle() . ' clone');
$duplicate
->save();
$destination = \Drupal::destination()
->get();
return new RedirectResponse($destination);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CalendarController:: |
protected | property | ||
CalendarController:: |
protected | property | Drupal\content_calendar\ContentTypeConfigService definition. | |
CalendarController:: |
protected | property | ||
CalendarController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
CalendarController:: |
public | function | Duplicate Node. | |
CalendarController:: |
public | function | Redirect and jump to a given Calendar directly. | |
CalendarController:: |
public | function | Redirect to current Calendar. | |
CalendarController:: |
public | function | Show Calendar year. | |
CalendarController:: |
public | function | Show Calendar year. | |
CalendarController:: |
public | function | Update creation date of a given Node. | |
CalendarController:: |
public | function | Constructs a new CalendarController object. | |
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |