In my bootstrap theme, I am trying to make the id of the paragraph the same as an attribute for each of the child nodes, so an accordion will work properly. I have tried looking at doing it through hooks, but I can't seem to access the paragraph data from my node hook.
1 Answer
You can use hook_preprocess_paragraph
hook to set the same id to all child nodes, in the below code snippet example all child nodes will have paragraph-PARAGRAPH-ID
as id.
use Drupal\Component\Utility\Html;
/**
* Implements hook_preprocess_HOOK().
*/
function myModule_preprocess_paragraph(&$variables) {
$paragraph = $variables['paragraph'];
$identifier = Html::cleanCssIdentifier('paragraph-' . $paragraph->id());
$variables['attributes']['id'] = $identifier;
}
-
How would I put the paragraph id in a different attribute like "data-parent" in the child node? Feb 25, 2019 at 18:32
-
try with
$variables['attributes']['data-parent'] = $identifier;
instead of$variables['attributes']['id'] = $identifier;
.– berramouFeb 25, 2019 at 19:04 -
I know I can do something like $variables['var_name'] to have it be a variable in the paragraph twig file. How would I do this to get the variable (in my case paragraph id) inside the children node twig files too? Feb 25, 2019 at 19:41