1

I want to add a content to content type (ct1) which has only one field (f1) which is a reference to a paragraph (p1), said paragraph has 4 fields: 3 numeric fields (1, 2, 3) and entity reference (er1) to taxonomy vocabulary, also i am supposed to check if term exists and if term exists then add content, otherwise throw a message and do not add content at all.

I'm Drupal 8 beginner and have no idea where to start with this task. Content creation for non-referenced content types is something I already can do but I do not know how to approach this particular situation. Please link me some guides or post example code.

I do not want to pre-fill paragraphs, it is more like "after-fill" when node is already created and i want to add paragraphs to it. Currently my code looks like this, this is more like pseudo-code, draft:

if (is_file($file) && is_readable($file) && in_array($_FILES['file']['type'], $mimes)) {
            $handle = fopen($file, 'r');

            $node = Node::create(array(
                        'type' => 'your_content_type',
                        'title' => date('l jS \of F Y h:i:s A'),
                        'langcode' => 'en',
                        'uid' => '1',
                        'status' => 1,
                        'field_fields' => array(),
            ));


            while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {

$termExists = taxonomy_term_load_multiple_by_name($field_taxonomy, 'taxonomy');
                if (isset($termExists)) {
                    $new_csvLine = Paragraph::create($csvLine);
                    $new_csvLine->save();
                    $node->paragraph_field_referenced->appendItem($new_csvLine);
                } else {
                    return print("Taxonomy term $field_taxonomy doesn't exist, import halted.");
                }
            }
            $node->save();

I'm working on csv import module and this is the only thing which left to me to complete. Loaded data I need to add as new node of ct1 and each line from csv needs to be a separate paragraph with 4 fields: 1, 2, 3 and er1. Now I need to somehow store this data in these fields. And yes, I need to do this all programmatically, in code. In other words: one csv import is a new node of ct1, every line of csv is new paragraph p1 and every paragraph has to store data from imported line from csv which is 4 fields.

How should I modify saving paragraphs part to make it work the way I described?

One more time about structure of all this:

content type ct1 -> field: f1, type: Entity reference revisions -> paragraph p1 -> fields: numeric 1, 2, 3; er1 type "Entity reference" (reference to taxonomy).

  • So you want create ct1 programmatically and reference it to a specific pargraph correct? Than you have to create a 5th field to your paragraph from type entity_reference->content than select your content type you want to reference. Now you can create your content , than you have to load the specific paragraph and set the reference on the paragraph like $paragraph->set('field_name', <content_id>); The content id is the target_id that link your paragraph to your content that you created. – Insasse Aug 9 '18 at 7:15
  • I'm working on csv import module and this is the only thing which left to me to complete. Loaded data i need to add as new node of ct1 and each line from csv needs to be a separate paragraph with 4 fields: 1, 2, 3 and er1. Now i need to somehow store this data in these fields. And yes, i need to do this all programmatically, in code. In other words: one csv import is a new node of ct1, every line of csv is new paragraph p1 and every paragraph has to store data from imported line from csv which is 4 fields. I hope this is a bit more clear now. Ask more questions if you need more clarification. – spectatorx Aug 9 '18 at 7:26
  • I do not want to pre-fill paragraphs, it is more like "after-fill" -> this is technically the same, you fill the paragraph field with content, which is in this case one or more paragraphs you've just created. – 4k4 Aug 9 '18 at 7:52
  • Ok, i agree with you but question is still open: how do i create a new node of content type and fill its referenced paragraph's fields with imported data? Node is ct1 and paragraphs are p1, fields are: 1, 2, 3, er1. I do not know how to access referenced paragraph and its fields. – spectatorx Aug 9 '18 at 7:57
  • There is no difference in filling fields of paragraphs or nodes. After you've found the term you can add it as field value to $values before you run Paragraph::create($values); – 4k4 Aug 9 '18 at 8:49

Your Answer

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

Browse other questions tagged or ask your own question.