views_xml_backend.module in Views XML Backend 8
Same filename and directory in other branches
Hook implementations for views_xml_backend.
File
views_xml_backend.moduleView source
<?php
/**
* @file
* Hook implementations for views_xml_backend.
*/
use Drupal\Core\Site\Settings;
use Drupal\views_xml_backend\Plugin\views\query\Xml;
/**
* Implements hook_cron().
*/
function views_xml_backend_cron() {
// Find old cache files and delete them. Default to one week.
$expiration = Settings::get('views_xml_backend_expire', 604800);
$directory = Settings::get('views_xml_backend_cache_directory', Xml::DEFAULT_CACHE_DIR);
$file_system = \Drupal::service('file_system');
if (!is_dir($directory)) {
return;
}
// Cache files are sha256 hashes without an extension.
foreach (file_scan_directory($directory, '/^[a-z0-9]+$/') as $file) {
if (!($mtime = filemtime($file->uri))) {
continue;
}
if ($mtime < REQUEST_TIME - $expiration) {
$file_system
->unlink($file->uri);
}
}
}
/**
* Returns a UNIX timestamp rounded to the specified granularity.
*
* @param string $date
* The date to convert.
* @param string $granularity
* The granularity.
*
* @return int
* The UNIX timestamp.
*/
function views_xml_backend_date($date, $granularity = 'second') {
$date = trim($date);
// strtotime() does not handle year values.
if (strlen($date) === 4 && (string) (int) $date === $date) {
$date = 'January 1, ' . $date;
}
$timestamp = is_numeric($date) ? $date : strtotime($date);
// Avoid calling getdate() for second granularity.
if ($granularity === 'second') {
return (int) $timestamp;
}
$parts = getdate((int) $timestamp);
switch ($granularity) {
case 'minute':
$parts['seconds'] = 0;
break;
case 'hour':
$parts['seconds'] = 0;
$parts['minutes'] = 0;
break;
case 'day':
$parts['seconds'] = 0;
$parts['minutes'] = 0;
$parts['hours'] = 0;
break;
case 'month':
$parts['seconds'] = 0;
$parts['minutes'] = 0;
$parts['hours'] = 0;
$parts['mday'] = 1;
break;
case 'year':
$parts['seconds'] = 0;
$parts['minutes'] = 0;
$parts['hours'] = 0;
$parts['mday'] = 1;
$parts['mon'] = 1;
break;
}
return (int) mktime($parts['hours'], $parts['minutes'], $parts['seconds'], $parts['mon'], $parts['mday'], $parts['year']);
}
/**
* Returns a formatted date value.
*
* @param string $date
* The date to convert.
* @param string $format
* The format to return.
*
* @return string
* The formatted date value.
*/
function views_xml_backend_format_value($date, $format) {
$date = trim($date);
// strtotime() does not handle year values.
if (strlen($date) === 4 && (string) (int) $date === $date) {
$date = 'January 1, ' . $date;
}
$timestamp = is_numeric($date) ? $date : strtotime($date);
return date($format, $timestamp);
}
Functions
Name | Description |
---|---|
views_xml_backend_cron | Implements hook_cron(). |
views_xml_backend_date | Returns a UNIX timestamp rounded to the specified granularity. |
views_xml_backend_format_value | Returns a formatted date value. |