0

So I have this code, using Guzzle POSTing to create node via Drupal REST API. No error, but the field_meta_tags value is empty on the node created (still using default [node:summary] for the meta description).

$node = array(
'title' => array(0 => array('value' => 'asd')),
'type' => array(0 => array('target_id' => 'page')),
'path' => array(0 => array('alias' => $pathalias)),
'field_meta_tags' => array(
  'title' => 'asd',
  'description' => 'asd',
  'keywords' => 'asd',
),
'body' => array(0 => array('value' => 'huehuehue')),
);

$response = $client->post($base_url . '/entity/node', array(
  'cookies' => $jar,
  'query' => array(
    '_format' => 'json',
  ),
  'headers' => array(
    'Accept' => 'application/json',
    'Content-Type' => 'application/json',
    'X-CSRF-Token' => $csrf_token,
  ),
  'json' => $node
));

I also try using array structure like this:

'field_meta_tags' => array(0 => array(
  'title' => 'asd',
  'description' => 'asd',
  'keywords' => 'asd,
)),

But still no luck.

What is the correct format to provide metatag value? Or is there another thing that I might need to check?

1 Answer 1

3

I copy/paste the following snippets from the readme file of the metatag module. I can see that you are missing the serialize function in your solution. Hopefully that helps you.

Programmatically assign meta tags to an entity
--------------------------------------------------------------------------------
There are two ways to assign an entity's meta tags in custom module. Both
scenarios require a "Metatag" field be added to the entity's field settings, the
field name "field_meta_tags" is used but this is completely arbitrary.

Option 1:

  $entity_type = 'node';
  $values = [
    'nid' => NULL,
    'type' => 'article',
    'title' => 'Testing metatag creation',
    'uid' => 1,
    'status' => TRUE,
    'field_meta_tags' => serialize([
      'title' => 'Some title',
      'description' => 'Some description.',
      'keywords' => 'Some,Keywords',
    ]),
  ];
  $node = \Drupal::entityTypeManager()->getStorage($entity_type)->create($values);
  $node->save();

Option 2:

  $node = Node::create(array(
    'type' => article,
    'langcode' => 'en',
    'status' => 1,
    'uid' => 1,
  ));
  $node->set('title', 'Testing metatag creation');
  $node->set('field_meta_tags', serialize([
    'title' => 'Some title',
    'description' => 'Some description.',
    'keywords' => 'Some,Keywords',
  ]));
  $node->save();

In both examples, the custom meta tag values will still be merged with the
values defined via the global defaults prior to being output - it is not
necessary to copy each value to the new record.
3
  • Thank you, that's it. Value needs to be serialized. 'field_meta_tags' => array(0 => array( 'value' => serialize(array( 'title' => 'asd', 'description' => 'asd', 'keywords' => 'asd', )), )),
    – Permana
    Mar 13, 2017 at 1:00
  • I am glad it worked. Best regards, Oleg Mar 13, 2017 at 2:00
  • Thank you very much, I've been trying many ways to do it so far! May I ask how did you figure it out? How do you usually find out how to update fields programmatically?
    – neojp
    Aug 17, 2018 at 7:16

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.