- Issue created by @Anybody
- Open on Drupal.org →Core: 10.2.1 + Environment: PHP 7.4 & MySQL 5.7last update
6 months ago Waiting for branch to pass - 🇩🇪Germany Grevil
This is actually needed for 🐛 Menu Link attributes overrides menu link options provided by other modules Closed: duplicate . The current implementation from 🐛 Compatibility with other modules, options being overwritten. Fixed doesn't work for micon menu.
- 🇩🇪Germany Grevil
"NestedArray::mergeDeep" is exactly what we need!
The problem with array_merge_recursive is, that if we have the exact same keys in both arrays, which contain a string, they will merge into an array:
$a = [ 'attributes' => ['target' => '_blank'], ]; $b = [ 'attributes' => ['target' => '_self'], ]; $c = array_merge_recursive($a, $b); var_dump($c);
will output:
~~~
array(1) {
["attributes"]=>
array(1) {
["target"]=> array(2) {
0 => "_blank"
1 => "_self"
}
}
}
~~~mergeDeep won't do that:
$link_options_1 = array( 'fragment' => 'x', 'attributes' => array( 'title' => t('X'), 'class' => array( 'a', 'b', ), ), ); $link_options_2 = array( 'fragment' => 'y', 'attributes' => array( 'title' => t('Y'), 'class' => array( 'c', 'd', ), ), ); // This results in array('fragment' => array('x', 'y'), 'attributes' => array('title' => array(t('X'), t('Y')), 'class' => array('a', 'b', 'c', 'd'))). $incorrect = array_merge_recursive($link_options_1, $link_options_2); // This results in array('fragment' => 'y', 'attributes' => array('title' => t('Y'), 'class' => array('a', 'b', 'c', 'd'))). $correct = NestedArray::mergeDeep($link_options_1, $link_options_2);
- Open on Drupal.org →Core: 10.2.1 + Environment: PHP 7.4 & MySQL 5.7last update
6 months ago Waiting for branch to pass - Status changed to Needs review
6 months ago 10:12am 14 May 2024 - 🇩🇪Germany Grevil
Only question remains, which options should be the dominant array for values, that are not arrays. I'd suggest the "menu_link_attributes" values (like currently implemented).
When merging values that are not both arrays, the latter value replaces the former rather than merging with it.
- 🇩🇪Germany Grevil
Just tested it, and it works as expected! :)
For example, I overwrite the "target" behavior of core and add a class to my menu link, while also providing the micon menu options, the output of
$menu_link->link->first()->options
will look like this (before and after merge):
- Status changed to RTBC
6 months ago 10:24am 14 May 2024 - 🇩🇪Germany Anybody Porta Westfalica
This is perfect and removes the pain from array_merge_recursive :) Thank you @Grevil and @Drupal :)
- Open on Drupal.org →Core: 10.2.1 + Environment: PHP 7.4 & MySQL 5.7last update
6 months ago Waiting for branch to pass - First commit to issue fork.
- Open on Drupal.org →Core: 10.2.1 + Environment: PHP 7.4 & MySQL 5.7last update
6 months ago Waiting for branch to pass -
jcnventura →
committed 74de8ece on 8.x-1.x authored by
Anybody →
Issue #3447050 by Grevil, Anybody, jcnventura: Need to deep merge...
-
jcnventura →
committed 74de8ece on 8.x-1.x authored by
Anybody →
- Status changed to Fixed
6 months ago 11:50am 14 May 2024 - 🇩🇪Germany Anybody Porta Westfalica
Niiiiiiiice, thank you so much @jcnventura! That was fast!
Automatically closed - issue fixed for 2 weeks with no activity.