public static function CalendarHelper::limitFormat in Calendar 8
Limits a date format to include only elements from a given granularity array.
Example: DateGranularity::limitFormat('F j, Y - H:i', ['year', 'month', 'day']); returns 'F j, Y'
Parameters
string $format: A date format string.
array $array: An array of allowed date parts, all others will be removed.
Return value
string The format string with all other elements removed.
1 call to CalendarHelper::limitFormat()
- template_preprocess_calendar_time_row_heading in ./
calendar.theme.inc - Format the time row headings in the week and day view.
File
- src/
CalendarHelper.php, line 771
Class
- CalendarHelper
- Defines Gregorian Calendar date values.
Namespace
Drupal\calendarCode
public static function limitFormat($format, array $array) {
// If punctuation has been escaped, remove the escaping. Done using strtr()
// because it is easier than getting the escape character extracted using
// preg_replace().
$replace = [
'\\-' => '-',
'\\:' => ':',
"\\'" => "'",
'\\. ' => ' . ',
'\\,' => ',',
];
$format = strtr($format, $replace);
$format = str_replace('\\T', ' ', $format);
$format = str_replace('T', ' ', $format);
$regex = [];
// Create regular expressions to remove selected values from string.
// Use (?<!\\\\) to keep escaped letters from being removed.
foreach ($array as $element) {
switch ($element) {
case 'year':
$regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[Yy])';
break;
case 'day':
$regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[l|D|d|dS|j|jS|N|w|W|z]{1,2})';
break;
case 'month':
$regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[FMmn])';
break;
case 'hour':
$regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[HhGg])';
break;
case 'minute':
$regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[i])';
break;
case 'second':
$regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[s])';
break;
case 'timezone':
$regex[] = '([\\-/\\.,:]?\\s?(?<!\\\\)[TOZPe])';
break;
}
}
// Remove empty parentheses, brackets, pipes.
$regex[] = '(\\(\\))';
$regex[] = '(\\[\\])';
$regex[] = '(\\|\\|)';
// Remove selected values from string.
$format = trim(preg_replace($regex, [], $format));
// Remove orphaned punctuation at the beginning of the string.
$format = preg_replace('`^([\\-/\\.,:\'])`', '', $format);
// Remove orphaned punctuation at the end of the string.
$format = preg_replace('([\\-/,:\']$)', '', $format);
$format = preg_replace('(\\$)', '', $format);
// Trim any whitespace from the result.
$format = trim($format);
// After removing the non-desired parts of the format, test if the only
// things left are escaped, non-date, characters. If so, return nothing.
// Using S instead of w to pick up non-ASCII characters.
$test = trim(preg_replace('(\\\\\\S{1,3})', '', $format));
if (empty($test)) {
$format = '';
}
return $format;
}