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'][2]['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].
Update
Here are some more steps I have tried (from this page):
Here is an example loop:
<?php
foreach ( $content['field_panels_content'] as $wrapper) {
foreach ( $wrapper['entity']['paragraphs_item'] as $item ) {
if ( !empty($item['field_body_text']) ) {
print render($item['field_body_text']);
}
}
}
?>
It produces the following results: Cannot use object of type ParagraphsItemEntity as array in foreach ( $wrapper['entity']['paragraphs_item'] as $item ) {
Here is a reworked version:
<?php
foreach ( $content['field_panels_content'] as $wrapper) {
foreach ( $wrapper['entity'].paragraphs_item as $item ) {
if ( !empty($item.field_body_text) ) {
print render($item.field_body_text);
}
}
}
?>
Error message: Fatal error: Cannot use object of type ParagraphsItemEntity as array in line "foreach ( $wrapper['entity'].paragraphs_item as $item ) {"
Editing the code so only field_body_text is in square brackets (e.g. $item['field_body_text']
), produces this error message:
Fatal error: Cannot use object of type ParagraphsItemEntity as array
Editing the code so only paragraphs_item is in square brackets (e.g. $wrapper['entity']['paragraphs_item']
), produces 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 array
Other Notes
The following code works perfectly:
<?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']); ?>
So surely it should be a simple case to refactor the code into a loop to get the desired result.
Please note
I don't want to use other modules.
I will be printing out all the field_body_text separately at the top of the tpl file.
I'll then display the whole paragraph (with the field_body_text
still in it) in the normal way.