can't create 1st referenced data with Node::create

Created on 10 April 2024, 6 months ago
Updated 1 May 2024, 5 months ago

The 1st data record of a referenced content cannot be imported/created (this was still ok in version 1)

$node = Node::create([
                      'type' => 'n_test_content_type',
                      'title' => 'Test Import ',
                    ]);    
                        $field_value = [0 => [
                                                'test1_taxonomy' => 61,
                                                'test2_text' => 'test id 2',
                                             ],
                                        1 => [
                                                'test1_taxonomy' => 62,
                                                'test2_text' => 'test id 3',
                                             ],
                                        2 => [
                                                'test1_taxonomy' => 63,
                                                'test2_text' => 'test id 4',
                                             ]];
                        $node->set('field_n_tct_test_data_field', $field_value);
                    
               
                  $node->save();

Warning: Object of class Drupal\taxonomy\Entity\Term could not be converted to int in Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::castValue() (line 2568 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php).

There is a test module in the appendix. Maybe that will help a little

This Modul create a content Type "Test Inhaltstyp" and Taxonomy Vocabulary: "Test Kategorie" with taxonomies.
Edit: "\test_import_data_field\src\Form\TestNewContent.php" change Taxonomy IDs (after install; dont use id 1) and submit this Form: "/admin/config/system/test_import_data_field". Now new content is created and the first content is empty or then exists with ID 1.

πŸ› Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany HeavyStoneHead

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

Comments & Activities

  • Issue created by @HeavyStoneHead
  • πŸ‡«πŸ‡·France lazzyvn paris

    in version 2 it supports rest and json api. you can use entity object (not number with tid) but i would try with number like version 1

  • πŸ‡«πŸ‡·France lazzyvn paris

    Test with devel PHP it works fine. I didn't check your module but i think there are small problem with your code

    use Drupal\node\Entity\Node;
    
    $node = Node::create(['type' => 'page','title' => 'Test Import ']);    
    $field_value = [ 
     ['taxo' => 1,'tex' => 'test id 1'], 
     ['taxo' => 1,'tex' => 'test id 2']
    ];
    $node->set('field_information', $field_value);
    $node->save();
    
  • πŸ‡©πŸ‡ͺGermany HeavyStoneHead

    I have a complex converter that imports individual records from an old database as Drupal content. This worked fine until the update to version 2. I tried to determine what the problem was and wrote the module so that it could be narrowed down. The problem is as described. So when you create and save again, the first data record is correct again. The β€œform” contains static data, so it’s not actually possible to say that something is going wrong. I have it on 2 different Drupal instances with the same effect.

  • πŸ‡«πŸ‡·France lazzyvn paris

    what version of Drupal are you using? since 10.2.x the field storage structure has changed in core, I need to change the definitions if you want to debug, see in \datafield\src\Plugin\Field\FieldType\DataField::propertyDefinitions and \datafield\src\Plugin\DataType\DataFieldEntityData.php
    it's different between 1.x and 2.x
    Have you tried Data Field dev version, maybe it has been fixed?

  • πŸ‡©πŸ‡ͺGermany HeavyStoneHead

    I'm using Drupal version: 10.2.5
    Yes, Dev works fine in the test instance. I'm not sure if I should test this again in the production version (backup). When would the next release be?

    In any case, I'm glad I could use dev if needed

    By the way, thanks for integrating it with JSON API

  • πŸ‡«πŸ‡·France lazzyvn paris

    I haven't finished new features search for entities reference yet. You can check the new optional "Reference field" in dev version. you will see a new magnifying glass icon, it will allow to select multiple entities like module entity browser
    I also want to support graphql in the next version. If you can help me I can release a new version very soon.

  • πŸ‡©πŸ‡ͺGermany HeavyStoneHead

    My skills are very manageable and probably wouldn't be any help.

    Thank you for the time you sacrificed for the great module.

  • Status changed to Closed: outdated 6 months ago
  • πŸ‡«πŸ‡·France lazzyvn paris
  • πŸ‡©πŸ‡ͺGermany HeavyStoneHead

    Version 2.0.2 is different from the developer. Same problem, only this time completely confusing.

    see screenshots

    old dev works fine, now its broken again and different.

  • Status changed to Needs work 6 months ago
  • πŸ‡«πŸ‡·France lazzyvn paris

    I don't understand much, could you explain in more detail

  • πŸ‡©πŸ‡ͺGermany HeavyStoneHead

    It is exactly the same as before that the 1st entry is not correct. Previously the taxonomy ID-1 appeared to have been inserted, but now it feels partly arbitrary. For the most part I have the feeling that it filters the type and takes the 1st ID of the vocabulary, on the other hand it is e.g. for country and city still incorrect vocabulary and with the ID 1.

    I suspect that he sets the first datafield with id 1, filters all the others by vocabulary and uses its first sorted ID. It's confusing and currently incomprehensible to me.

    I still have a backup of the working dev, is there any way to show what's different? I'll add that.

  • πŸ‡©πŸ‡ͺGermany HeavyStoneHead

    I think I found the part

    \datafield-2.0.2\datafield\src\Plugin\Field\FieldType\DataField.php
    Line: 608
    Missing:

          if ($item['type'] == 'entity_reference' && empty($value)) {
            return TRUE;
          }
    

    After I added the part to the current one, everything worked perfectly again.

    • f422c34e committed on 2.x
      Issue #3439931: can't create 1st referenced data with Node::creat
      
  • πŸ‡«πŸ‡·France lazzyvn paris

    I see check dev version

  • πŸ‡©πŸ‡ͺGermany HeavyStoneHead

    its works

  • Status changed to Fixed 5 months ago
  • πŸ‡«πŸ‡·France lazzyvn paris
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024