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

UHF-8926 #460

Merged
merged 15 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
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
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions public/modules/custom/helfi_group/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Group-navigation changes

This instance has some navigation changes that differs from main navigation.

### Node - published in main navigation

Nodes that are published in group's navigation, the "published in main navigation" button is set to follow the node's
translation status. This allows the school editors to handle the menu translations in simpler way.
53 changes: 53 additions & 0 deletions public/modules/custom/helfi_group/helfi_group.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* @file
* Contains installation functions for Kasko instance.
*/

declare(strict_types = 1);

use Drupal\Core\Url;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\node\Entity\Node;

/**
* Update group menu item's content translation status to match node's status.
*/
function helfi_group_update_9001(): void {
$menu_name_prefix = 'group_menu_link_content-';

$query = \Drupal::entityQuery('menu_link_content')
->condition('menu_name', $menu_name_prefix, 'CONTAINS')
->sort('id', 'ASC')
->accessCheck(FALSE);
$result = $query->execute();
if (!$result) {
return;
}

$menu_items = MenuLinkContent::loadMultiple(array_values($result));
$languages = ['fi', 'en', 'sv'];

foreach ($menu_items as $menu_item) {
$params = Url::fromUri($menu_item->get('link')->uri)->getRouteParameters();
$node_id = $params['node'];
foreach ($languages as $langcode) {
if (!$menu_item->hasTranslation($langcode)) {
continue;
}

$menu_item = $menu_item->getTranslation($langcode);
$node = Node::load($node_id);
if (!$node->hasTranslation($langcode)) {
continue;
}

$node = $node->getTranslation($langcode);
if ((bool) $menu_item->content_translation_status->value !== $node->isPublished()) {
$menu_item->set('content_translation_status', $node->isPublished())->save();
}

}
}
}
120 changes: 120 additions & 0 deletions public/modules/custom/helfi_group/helfi_group.module
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\helfi_group\UnitFormAlter;
use Drupal\menu_link_content\Entity\MenuLinkContent;

/**
* Implements hook_preprocess_HOOK().
Expand Down Expand Up @@ -153,3 +154,122 @@ function helfi_group_node_create_access(AccountInterface $account, array $contex
? AccessResult::allowed()
: AccessResult::neutral();
}

/**
* Implements hook_form_BASE_FORM_ID_alter().
*
* #UHF-8926 Group menu emits menu parent from entity's translation form.
* Make sure menu_parent is always set.
*/
function helfi_group_form_menu_link_content_form_alter(&$form, FormStateInterface $form_state) {
$entity = $form_state->getFormObject()->getEntity();
$menu_name = $entity->get('menu_name')->value;
if (
!$menu_name ||
!str_contains($menu_name, 'group') ||
isset($form['menu_parent'])
) {
return;
}

$form['menu_parent']['#type'] = 'hidden';
rpnykanen marked this conversation as resolved.
Show resolved Hide resolved
$menu_parent = sprintf('%s:%s', $menu_name, $entity->get('parent')->value);
$form['menu_parent']['#value'] = $menu_parent;
}

/**
* Implements hook_module_implements_alter().
*
* #UHF-8926 the form alter must be run after group content menu's alter.
rpnykanen marked this conversation as resolved.
Show resolved Hide resolved
* Otherwise the changes made in "menu"-render array would be overridden.
*/
function helfi_group_module_implements_alter(&$implementations, $hook) : void {
if ($hook === 'form_alter' && isset($implementations['helfi_group'])) {
$group = $implementations['helfi_group'];
unset($implementations['helfi_group']);
$implementations['helfi_group'] = $group;
}
}

/**
* Implements hook_form_node_form_alter().
*/
function helfi_group_form_node_form_alter(
array &$form,
FormStateInterface $form_state
): void {
$form['actions']['submit']['#submit'][] = 'helfi_group_menuitem_set_content_translation_status';

if ($form['menu']) {
$menu_parent = $form['menu']['link']['menu_parent']['#default_value'];
$menu_name = explode(':', $menu_parent)[0];

if (!str_contains($menu_name, 'group')) {
return;
}

$result = \Drupal::entityQuery('menu_link_content')
->condition('link.uri', "entity:node/{$form_state->getFormObject()->getEntity()->id()}")
->condition('menu_name', [$menu_name], 'IN')
->sort('id', 'ASC')
->accessCheck(FALSE)
->range(0, 1)
->execute();

if (!$result) {
return;
}

$current_language = $form_state
->getFormObject()
->getEntity()
->language()
->getId();

$menu_link_content_id = reset($result);
$menu_link_content = MenuLinkContent::load($menu_link_content_id);
if (!$menu_link_content || !$menu_link_content->hasTranslation($current_language)) {
return;
}

$menu_link_content = $menu_link_content->getTranslation($current_language);
$status = $menu_link_content->get('content_translation_status')->value;
$form['menu']['content_translation_status']['#default_value'] = $status;
$form["menu"]["content_translation_status"]["#description"] = t('Tämä arvo muuttuu automaattisesti tallentaessa sisällön julkaisutilan mukaan.');
}

}

/**
* Submit handler to set menu items translation status.
*
* @param array $form
* The form.
* @param Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function helfi_group_menuitem_set_content_translation_status(
array &$form,
FormStateInterface $form_state
) : void {
$menu_parent = $form['menu']['link']['menu_parent']['#default_value'];
$menu_name = explode(':', $menu_parent)[0];
$values = $form_state->getValue('menu');
$node = $form_state->getFormObject()->getEntity();
$langCode = $node->language()->getId();
$link = MenuLinkContent::load($values['entity_id']);
if (
empty($values['entity_id']) ||
!$link ||
!str_contains($menu_name, 'group')
) {
return;
}
if ($link->hasTranslation($langCode)) {
$link = $link->getTranslation($langCode);
$link->set('content_translation_status', $form_state->getValue('status')['value'])
->save();
}
}