Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convivial_bootstrap-34140: Make code more defensive. #46

Merged
merged 1 commit into from
Sep 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 52 additions & 48 deletions convivial_bootstrap.theme
Original file line number Diff line number Diff line change
Expand Up @@ -237,37 +237,39 @@ function convivial_bootstrap_preprocess_layout(&$variables) {

// Replace calendar item date field content by custom template.
if ($layout_id === 'calendar_item') {
foreach (Element::children($variables['content']['date']) as $name) {
$block = &$variables['content']['date'][$name];
if (!empty($block['content']['#field_name']) && $entity && $entity->get($block['content']['#field_name'])) {
$field = $entity->get($block['content']['#field_name']);
$timestamp = NULL;

switch ($block['content']['#field_type']) {
case 'datetime':
case 'daterange':
$timestamp = strtotime($field->value);
break;

case 'timestamp':
case 'created':
case 'changed':
$timestamp = $field->value;
break;
}
if (isset($variables['content']['date'])) {
foreach (Element::children($variables['content']['date']) as $name) {
$block = &$variables['content']['date'][$name];
if (!empty($block['content']['#field_name']) && $entity && $entity->get($block['content']['#field_name'])) {
$field = $entity->get($block['content']['#field_name']);
$timestamp = NULL;

switch ($block['content']['#field_type']) {
case 'datetime':
case 'daterange':
$timestamp = strtotime($field->value);
break;

case 'timestamp':
case 'created':
case 'changed':
$timestamp = $field->value;
break;
}

if (!empty($timestamp)) {
$datetime = new DrupalDateTime();
$datetime->setTimestamp($timestamp + $datetime->getOffset());
$block['content'][0] = [
'#theme' => 'calendar_item_date',
'#datetime' => $datetime,
'#attached' => [
'library' => [
'convivial_bootstrap/calendar-item',
if (!empty($timestamp)) {
$datetime = new DrupalDateTime();
$datetime->setTimestamp($timestamp + $datetime->getOffset());
$block['content'][0] = [
'#theme' => 'calendar_item_date',
'#datetime' => $datetime,
'#attached' => [
'library' => [
'convivial_bootstrap/calendar-item',
],
],
],
];
];
}
}
}
}
Expand All @@ -287,29 +289,31 @@ function convivial_bootstrap_preprocess_layout(&$variables) {
) {
$clickable = FALSE;

foreach (Element::children($variables['content']['title']) as $name) {
$block = &$variables['content']['title'][$name];

// Check if item is linked to some URL.
if (array_key_exists('content', $block) && array_key_exists('#field_name', $block['content'])) {
if ($block['content']['#field_name'] === 'field_heading') {
$item = NULL;
// Only if URL field is available.
if ($entity->id() !== NULL && $entity->hasField('field_item_url')) {
$item = $entity->get('field_item_url')->first();
if (isset($variables['content']['title'])) {
foreach (Element::children($variables['content']['title']) as $name) {
$block = &$variables['content']['title'][$name];

// Check if item is linked to some URL.
if (array_key_exists('content', $block) && array_key_exists('#field_name', $block['content'])) {
if ($block['content']['#field_name'] === 'field_heading') {
$item = NULL;
// Only if URL field is available.
if ($entity->id() !== NULL && $entity->hasField('field_item_url')) {
$item = $entity->get('field_item_url')->first();
}
if (!empty($item)) {
$block['content']['#item_url'] = $item->getUrl();
$clickable = TRUE;
}
}
if (!empty($item)) {
$block['content']['#item_url'] = $item->getUrl();

// Check if node title is linked to entity.
elseif ($block['content']['#field_name'] === 'title'
&& !empty($block['#configuration']['formatter']['settings']['link_to_entity'])
) {
$clickable = TRUE;
}
}

// Check if node title is linked to entity.
elseif ($block['content']['#field_name'] === 'title'
&& !empty($block['#configuration']['formatter']['settings']['link_to_entity'])
) {
$clickable = TRUE;
}
}
}

Expand Down