I am using Drupal Paragraphs module that lets you create "bundles" of fields inside other fields. (It does this using entities). Each bundle has it's own tpl.php file.
In addition, you can nest "sub-bundles" inside the "main-bundles" for complex layouts.
I have created a "main-bundle" called field_panels_content.
Inside this is another "subbundle" which holds a field_body_text. The "main-bundle" can hold an unlimited number of "sub-bundles".
In my theme, I need to access the all field_body_text fields from the multiple "subbundle" from the tpl.php file that is for the "main-bundle".
I have used devel_themer and found I can get to the field_body_text fields like this:
<?php print render($content['field_panels_content'][0]['entity']['paragraphs_item'][9]['field_body_text']); ?>
<?php print render($content['field_panels_content'][1]['entity']['paragraphs_item'][10]['field_body_text']); ?>
<?php print render($content['field_panels_content'][2]['entity']['paragraphs_item'][12]['field_body_text']); ?>As you can see this does not make for efficient theming as I would need to know the second array value in order to print it out (e.g. [9][10][12]).
Is there away I can use a loop or some other PHP function to get to the second array value automatically without knowing it?
This second array value changes with each new piece of content (e.g. If I add a new subbundle it will have a value of [13]. The reason the numbers jump from [10] to [12] is because I deleted the subbundle that was [11].
Comments
I would do something like
I would do something like
Thanks for your help! I tried
Thanks for your help! I tried your code and get this error message:
Cannot use object of type ParagraphsItemEntity as array inIt is referring to this line:
foreach ( $wrapper['entity']['paragraphs_item'] as $item ) {I don't understand why it doesn't work, as the following does work:
<?php print render($content['field_panels_content'][0]['entity']['paragraphs_item'][9]['field_body_text']); ?>.In the above code, the
['entity']['paragraphs_item']bit works fine.Thanks for any help you can offer!
That suggests
That suggests
should be
And/or
should be
Thanks for your help! Here
Thanks for your help! Here is the code:
But it comes with this error message:
Fatal error: Cannot use object of type ParagraphsItemEntity as array in line "foreach ( $wrapper['entity'].paragraphs_item as $item ) {"If I edit the code so only field_body_text is in square brackets (e.g.
$item['field_body_text']), I get this error message:Fatal error: Cannot use object of type ParagraphsItemEntity as arrayIf I edit the code so only paragraphs_item is in square brackets (e.g.
$wrapper['entity']['paragraphs_item']), I get this error message:Fatal error: Only variables can be passed by reference in line print render($item.field_body_text);If I use print instead of render, I get this error message:
Fatal error: Cannot use object of type ParagraphsItemEntity as arrayThanks for any help you can offer.
I usually take a different
I usually take a different approach and use the display suite module. Using it I would make a new view mode, lets call it "Only Body Text", then in the paragraph type(s) enable the new view mode and then for that view mode only enable the "body text". Then in the parent where you display the paragraphs, I would display them using the new view mode.
Thanks for your help! On my
Thanks for your help! On my Paragraph entity, I need the nested field to appear twice. Once as body_text only and then again with the full content of the full nested field.
Normally in Display Suite, you use a dynamic field to duplicate fields with a different view. Unfortunately, this doesn't work with Paragraph entities.
Plus, the rest of my theme is all based around tpl.php files (which in my opinion are much easier to edit than digging around DS options), so I'd rather not use DS for this one little feature.
However, thank you greatly for taking the time to help me, as I highly appreciate it.
The fields will print out as desired when using
<?php print render($content['field_panels_content'][2]['entity']['paragraphs_item'][12]['field_body_text']); ?>So it is a mystery as to why they don't work when the numbered arrays are replaced with 'for loops'.