0

i need to make an import from one mysql table in my base.

I create node with :

$value= array(
                'type' => strtolower($typeProduit),
                'field_1'=>"test",
                'title'=>"test",
                'body' => array(
                    'value' => "test",
                    'format' => 'full_html',
                ),
                'field_2'=>"",
                'field_3'=>"",
                'field_4'=>"",                
            );

 $nodeEntity=\Drupal::entityTypeManager()
        ->getStorage('node')
        ->create($value);

    $nodeEntity->save();

All node are create and if i have an image, i add it to my $value before save() :

$value['field_image_video']=array(
                    'target_id' => $fileImageVideo->id(),
                    'alt' => $catalogue->titreVideo,
                    'title' => $catalogue->titreVideo,
                );

if i have no image, i do nothing for this field.

I need to update some node programmatically after this first step.

And when i just try to change the title like this :

     $nodeEntityUpdate = \Drupal::entityTypeManager()
                    ->getStorage('node')                
                    ->load(273);

     $nodeEntityUpdate->title="blabla";
     $nodeEntityUpdate->save();

if node don"t have image field set, i got this error :

PHP Fatal error: Call to a member function getFileUri()

If node has image, update work.

How can i set my image_field to null ?

i try to set my image field like that on creation :

$value['field_image_video']=array();

But that didn't work.

this image field is set to unlimited values

1 Answer 1

0

i got it in the update :

i set my image_field in the update:

  $nodeEntityUpdate = \Drupal::entityTypeManager()
                    ->getStorage('node')                
                    ->load(273);

     $nodeEntityUpdate->title="blabla";
     $nodeEntityUpdate->field_image_video =array();
     $nodeEntityUpdate->save();

I try to set this field in the create, but that change nothing.

As i can see, we need to set image field in the update if she is empty at the creation.

2
  • $node->set($node->getEntityKey('label'), 'blabla')->set('field_image_video', NULL)->save(); or possibly $node->get('field_image_video')->filter(TRUE).
    – user21641
    Aug 23, 2017 at 8:47
  • yes but we need absolutly set the field_image_video ?
    – Kevin
    Aug 23, 2017 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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