Skip to content

Instantly share code, notes, and snippets.

@mortendk
Created April 22, 2014 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mortendk/11191799 to your computer and use it in GitHub Desktop.
Save mortendk/11191799 to your computer and use it in GitHub Desktop.
merging attributes
$variables['attributes']['class'] = 'test-attribute-merge-1';
$variables['content_attributes']['class'] ='test-content_attributes-merge-2';
$variables['attributes']['role'] = 'attributes-role';
$variables['content_attributes']['role'] = 'content_attributes-role';
$variables['title_attributes']['role'] = 'title-attributes-role';
$variables['item_attributes']['0']['role'] = 'item-attributes-role';
//wtf why u not merge
$variables['attributes_test'] = NestedArray::mergeDeepArray(array(
$variables['attributes'],
$variables['content_attributes'],
$variables['title_attributes']
)
);
@kitsunet
Copy link

What do you expect as result? Same keys will override with priority to the last given argument. You won't get a joined 'roles' key with all roles that you assigned if that is what you are looking for.

At least that is what I get from a quick look at the method.

@mortendk
Copy link
Author

yup thats exactly what i was getting

@kitsunet
Copy link

Now I don't know the Drupal API but my guess is that there is no method for what you need. Would be pretty useful for a web CMS that has to deal with HTML attributes. :)

Edit: Rereading this might come over a bit unfriendly but isn't meant that way. This kind of merging is pretty difficult because it's hard to define what you expect. Basically for this kind of merging you can only reasonably work with one level of nesting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment