Error if have more that 1 paragraph selected in a field

Created on 22 October 2024, 2 months ago

Problem/Motivation

I want to import multiple components in a content section. So I want the user to be able to select the paragraph from a list and add to the content field.

Steps to reproduce

Create a paragraph reference field and select more that paragraph on it.
Try to map to 2 different paragraphs and import.
This error will show up:

Error: Attempt to assign property "field_xxx" on null in Drupal\feeds_para_mapper\Importer->setValue() (line 138)

Proposed resolution

Set the parent paragraph on the code?!

🐛 Bug report
Status

Active

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @saahbaa
  • First commit to issue fork.
  • I did some changes to debug the function:

    private function setValue($paragraph, $value) {
    $target = $this->target->getName();

    // Check if paragraph is null
    if ($paragraph === null) {
    \Drupal::logger('feed_para_mapper')->error("Paragraph is null.");
    return; // or handle the error
    }

    // Check if paragraph is an instance of EntityInterface
    if (!$paragraph instanceof \Drupal\Core\Entity\EntityInterface) {
    \Drupal::logger('feed_para_mapper')->error("Invalid paragraph entity.");
    return; // or handle the error
    }

    // Check if target property exists
    if (property_exists($paragraph, $target)) {
    $paragraph->{$target} = NULL;
    // Log the current value
    \Drupal::logger('feed_para_mapper')->info("Current value: @value", ['@value' => print_r($paragraph->{$target}, true)]);
    } else {
    // Handle case where the target property does not exist
    \Drupal::logger('feed_para_mapper')->warning("Target property does not exist: @target", ['@target' => $target]);
    }

    // Set the target value
    $this->instance->setTarget($this->feed, $paragraph, $target, $value);
    }

    so now it will import the ONLY the first referenced paragraph ...
    but still the issue exist if you want to map more than 1 referenced paragraph

  • now I am trying to create the paragraph:

    private function setValue($paragraph, $value) {
    $target = $this->target->getName();

    // Check if the paragraph is null
    if ($paragraph === null) {
    // Prepare host info
    $host_info = [];
    $entity = $this->entity; // Use the current entity as the parent
    $host_info['bundle'] = 'paragraph'; // Use the method to get the bundle
    $host_info['field'] = $target; // Use the target as the field
    $host_info['entity'] = $entity;

    // Call createParagraph to create a new paragraph
    $paragraph = $this->createParagraph($host_info['field'], $host_info['bundle'], $host_info['entity']);
    \Drupal::logger('feed_para_mapper')->info('Paragraph type: @type', ['@type' => $paragraph->getType()]);
    } else {
    // Reset the values of the target if paragraph already exists
    $paragraph->{$target} = NULL;
    \Drupal::logger('feed_para_mapper')->info('Paragraph type: @type', ['@type' => $paragraph->getType()]);
    }

    // Set the target and prepare values
    $this->instance->setTarget($this->feed, $paragraph, $target, $value);
    }

  • Is there any update on this please?
    I really need to import more than 1 paragraph on a same parent field. But it always imports only 1

  • It always imports only 1 paragraph into the same parent field

    Scenario is:

    Content type : Page
    Field: Content (has multiple paragraph references including text, related content, documents)
    it only imports the first reference. if if I first map text target it only imports that and ignore the next one which is related content
    If I map related content first it will import that and ignores the text
    if I call explode() in the beginning of the import() function it will only import the last target

Production build 0.71.5 2024