Skip to content

Commit

Permalink
Merge pull request #735 from City-of-Helsinki/UHF-9738-news-article
Browse files Browse the repository at this point in the history
UHF-9738: news article content type
  • Loading branch information
khalima authored Apr 11, 2024
2 parents cb23379 + 7cb38fa commit 79b7839
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 58 deletions.
1 change: 1 addition & 0 deletions .sonarcloud.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sonar.exclusions=**/*.api.php
12 changes: 7 additions & 5 deletions helfi_platform_config.module
Original file line number Diff line number Diff line change
Expand Up @@ -252,19 +252,21 @@ function helfi_platform_config_block_access(Block $block, $operation, AccountInt

if (
!$entity instanceof ContentEntityInterface ||
!$entity->hasField('field_has_hero')
!$entity->hasField('field_hero')
) {
return AccessResult::neutral();
}

// Show title block if the "has hero" checkbox is not checked (=false).
if (!$entity->get('field_has_hero')->value) {
if (
$entity->hasField('field_has_hero') &&
$entity->get('field_has_hero')->value
) {
return AccessResult::allowed()->addCacheableDependency($block);
}

// Show title block if the "has hero" checkbox is checked (=true) and
// the Hero paragraph is not found.
return AccessResult::allowedIf(!$entity->get('field_hero')->entity)
// Show title block if the Hero paragraph is not found.
return AccessResult::forbiddenIf($entity->get('field_hero')->entity)
->addCacheableDependency($block);
}

Expand Down
54 changes: 14 additions & 40 deletions modules/hdbt_admin_tools/hdbt_admin_tools.module
Original file line number Diff line number Diff line change
Expand Up @@ -249,21 +249,6 @@ function hdbt_admin_tools_get_page_entity(): ?EntityInterface {
return NULL;
}

// @codingStandardsIgnoreStart
/**
* Gets the current page main entity.
*
* @deprecated in helfi_platform_config:3.0.0 and is removed from helfi_platform_config:3.2.0.
* Use hdbt_admin_tools_get_page_entity() instead.
*
* @return \Drupal\Core\Entity\EntityInterface
* Current page main entity.
*/
function hdbt_content_get_page_entity(): ?EntityInterface {
return hdbt_admin_tools_get_page_entity();
}
// @codingStandardsIgnoreEnd

/**
* Implements hook_preprocess_HOOK().
*/
Expand All @@ -276,9 +261,11 @@ function hdbt_admin_tools_preprocess_page(&$variables): void {

// Set has_hero variable according to field_has_hero and existence of
// field_hero reference.
if ($entity->hasField('field_has_hero')) {
$variables['has_hero'] = $entity->get('field_has_hero')->value &&
!$entity->get('field_hero')->isEmpty();
if ($entity->hasField('field_hero')) {
$variables['has_hero'] = !$entity->get('field_hero')->isEmpty() && (
!$entity->hasField('field_has_hero') ||
$entity->get('field_has_hero')->value
);
}

// Handle sidebar visibility.
Expand All @@ -295,7 +282,6 @@ function hdbt_admin_tools_preprocess_page(&$variables): void {
* Content entity, like tpr_service, tpr_unit or node.
*/
function hdbt_admin_tools_handle_sidebar_visibility(array &$variables, ContentEntityInterface $entity): void {

// The entities that need to be handled listed as content type => entity type.
$allowed_entities = [
'page' => 'node',
Expand All @@ -304,10 +290,12 @@ function hdbt_admin_tools_handle_sidebar_visibility(array &$variables, ContentEn
'tpr_service' => 'tpr_service',
];

/** @var \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler */
$moduleHandler = Drupal::service('module_handler');

// Trigger hook_sidebar_visibility_allowed_entities_alter().
// Allow modules to alter the list of allowed entities.
\Drupal::service('module_handler')
->alter('sidebar_visibility_allowed_entities', $allowed_entities);
$moduleHandler->alter('sidebar_visibility_allowed_entities', $allowed_entities);

// Get possible (node) content type.
$content_type = $entity instanceof NodeInterface ? $entity->getType() : FALSE;
Expand All @@ -322,6 +310,9 @@ function hdbt_admin_tools_handle_sidebar_visibility(array &$variables, ContentEn
return;
}

$variables['has_sidebar_first'] = FALSE;
$variables['has_sidebar_second'] = FALSE;

// Load menu links for the current page entity.
$menu_link_manager = Drupal::service('plugin.manager.menu.link');
$menu_links = $menu_link_manager->loadLinksByRoute(
Expand All @@ -330,9 +321,6 @@ function hdbt_admin_tools_handle_sidebar_visibility(array &$variables, ContentEn
'main'
);

$variables['has_sidebar_first'] = FALSE;
$variables['has_sidebar_second'] = FALSE;

// If there are links in current language, apply "has_sidebar_first" variable
// to indicate twig templates how to render the sidebar.
// However, if the menu link is set to first level, do not render the
Expand Down Expand Up @@ -385,24 +373,10 @@ function hdbt_admin_tools_handle_sidebar_visibility(array &$variables, ContentEn
if ($entity instanceof Service && !$entity->get('links')->isEmpty()) {
$variables['has_sidebar_second'] = TRUE;
}
}

// @codingStandardsIgnoreStart
/**
* Handle sidebar visibility based on current entity menu links.
*
* @deprecated in helfi_platform_config:3.0.0 and is removed from helfi_platform_config:3.2.0.
* Use hdbt_admin_tools_handle_sidebar_visibility() instead.
*
* @param array $variables
* Variables array.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* Content entity, like tpr_service, tpr_unit or node.
*/
function _hdbt_content_handle_sidebar_visibility(array &$variables, ContentEntityInterface $entity): void {
hdbt_admin_tools_handle_sidebar_visibility($variables, $entity);
// Allow modules to override sidebar visibility.
$moduleHandler->alter('sidebar_visibility', $variables, $entity);
}
// @codingStandardsIgnoreEnd

/**
* Implements hook_preprocess_HOOK().
Expand Down
7 changes: 6 additions & 1 deletion modules/helfi_paragraphs_hero/helfi_paragraphs_hero.api.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
*
* @param array $designs
* The designs for the hero.
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition
* Hero design field.
* @param \Drupal\Core\Entity\FieldableEntityInterface|null $entity
* Current entity.
*/
function hook_helfi_hero_design_alter(array &$designs) {
function hook_helfi_hero_design_alter(array &$designs, \Drupal\Core\Field\FieldStorageDefinitionInterface $definition, \Drupal\Core\Entity\FieldableEntityInterface|null $entity) {

}
4 changes: 2 additions & 2 deletions modules/helfi_paragraphs_hero/helfi_paragraphs_hero.module
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function helfi_paragraphs_hero_entity_bundle_info_alter(array &$bundles): void {
* @return int[]
* Values to select.
*/
function helfi_paragraphs_hero_design_allowed_values(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL) : array {
function helfi_paragraphs_hero_design_allowed_values(FieldStorageDefinitionInterface $definition, ?FieldableEntityInterface $entity = NULL) : array {
$designs = [
'without-image-left' => t('Without image, align left'),
'with-image-right' => t('Image on the right'),
Expand All @@ -38,6 +38,6 @@ function helfi_paragraphs_hero_design_allowed_values(FieldStorageDefinitionInter
$moduleHandler = Drupal::service('module_handler');

// Let other modules alter the CKEditor link dialog form validation.
$moduleHandler->alter('helfi_hero_design', $designs);
$moduleHandler->alter('helfi_hero_design', $designs, $definition, $entity);
return $designs;
}
22 changes: 12 additions & 10 deletions src/Plugin/Block/HeroBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@ public function build() : array {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
['entity' => $entity, 'entity_version' => $entity_version] = $this->getCurrentEntityVersion();

// No need to continue if current entity doesn't have has_hero field.
// No need to continue if current entity doesn't have hero field.
if (
!$entity instanceof ContentEntityInterface ||
!$entity->hasField('field_has_hero')
!$entity->hasField('field_hero')
) {
return $build;
}

// @todo Support preview on entity reference fields ie. paragraphs.
if (!$entity->get('field_has_hero')->isEmpty()) {
if (
!$entity->hasField('field_has_hero') ||
!$entity->get('field_has_hero')->isEmpty()
) {
$first_paragraph_grey = '';

// Handle only landing page.
Expand All @@ -51,13 +54,12 @@ public function build() : array {
'unit_search',
'service_list_search',
];
foreach ($paragraphs_with_grey_bg as $paragraph_with_grey_bg) {
if (
$paragraph instanceof ParagraphInterface &&
$paragraph->getType() === $paragraph_with_grey_bg
) {
$first_paragraph_grey = 'has-first-gray-bg-block';
}

if (
$paragraph instanceof ParagraphInterface &&
in_array($paragraph->getType(), $paragraphs_with_grey_bg)
) {
$first_paragraph_grey = 'has-first-gray-bg-block';
}
}

Expand Down

0 comments on commit 79b7839

Please sign in to comment.