Basically, i am programmatically generating a node. Those nodes have several anidated paragraphs like this:

-title parent
 -body parent
 -links parent
 -title child
   -body child
   -links child

-another parent

I'm doing this with paragraphs, i have a parent paragraph that includes another. At the time of generating the node i'm generating in a sufficient manner the parent paragraph with the following code: ('field_contenido_padre' is the field that contains the parent paragraph)

$paragraph = new ParagraphsItemEntity(
        'field_name' => 'field_contenido_padre',
        'bundle' => 'acordeon'));
$paragraph->is_new = TRUE;
$paragraph->setHostEntity('node', $parent);
$paragraph->field_titulo_acordeon[LANGUAGE_NONE][0]['value'] = $value["title"];
$paragraph->field_body[LANGUAGE_NONE][0]['value'] = "jajaja";

$ia = 0;
foreach ($value["items"] as $k => $item){
    $paragraph->field_link_archivo[LANGUAGE_NONE][$ia]['title'] = $item;
    $paragraph->field_link_archivo[LANGUAGE_NONE][$ia]['url'] = $value["items"][((int) $k + 1)];

OK. this works. Following further from here is my problem. Now i would like to attach a child paragraph to this recently generated parent paragraph. I've tried the following with no success:

  1. first, generate the child paragraph ('field_acordeon_hijo' is the field in parent paragraph that contains the child)
$paragraph = new ParagraphsItemEntity(
      'field_name' => 'field_acordeon_hijo',
      'bundle' => 'acordeon-hijo'));
$paragraph->is_new = TRUE;
$paragraph->setHostEntity('paragraphs_item', $parent);
$paragraph->field_titulo_acordeon_hijo[LANGUAGE_NONE][0]['value'] ='TITULAko Hijo';
$paragraph->body[LANGUAGE_NONE][0]['value'] = 'jajaja';
  1. Now i try to save the recent child into the parent's container field:

     $entity = entity_load('paragraphs_item', array($parent->item_id));  
     $entity = reset($entity);  
     $entity->field_acordeon_hijo[LANGUAGE_NONE] = array(
         0 => array(
           'value' => $id_child,
           'safe_value' => $id_child,
           'format ' => NULL));   
     $entity->revision = TRUE;   

I get no errors, the parent paragraph is generated fine, but i don't see the children paragraphs :-/

I guess it's because I'm attaching the child to the parent, while maybe I must attach it to the node; but in that case, how to point the child to the parent?


After two days, finally, i got it working. Step 3 is not necessary. My error was in step 2, where I wrote badly the bundle name :-( so it was

new ParagraphsItemEntity(
      'field_name' => 'field_acordeon_hijo',
      'bundle' => 'acordeon_hijo'))

note the underscore in acordeon_hijo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.