Skip to content

Commit

Permalink
convivial-demo-34140: Make code more defensive. (#46)
Browse files Browse the repository at this point in the history
Mirrored from morpht/convivial-demo@a5f2b7c (34140-bug).

Co-authored-by: Jan Pára <[email protected]>
  • Loading branch information
radimklaska and honzapara committed Sep 9, 2023
1 parent 08eb174 commit 6d8b8e9
Showing 1 changed file with 52 additions and 48 deletions.
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

0 comments on commit 6d8b8e9

Please sign in to comment.