I am programmatically generating nodes and need to override the metatag defaults. I have added the metatag field to the content type but I do not see in the docs how to do this.

I'm willing to help update the docs once I figure out how to do this.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nicxvan created an issue. See original summary.

DamienMcKenna’s picture

Title: Programmatically override a metatag when generating a node. (documentation) » Programmatically override a metatag when generating a node
Category: Support request » Task

Lets document this properly for both branches.

DamienMcKenna’s picture

So, lets look at the basics:

https://gaiaes.com/article/create-a-node-programmatically-drupal-8

$values = array(
  'nid' => NULL,
  'type' => 'article',
  'title' => 'Create node using Node storage create method',
  'uid' => 1,
  'status' => TRUE,
);
$node = \Drupal::entityManager()->getStorage('node')->create($values);
$node->save();
DamienMcKenna’s picture

entityManager() was replaced with entityTypeManager(), so it should be:

$entity_type = 'node';
$values = [
  'nid' => NULL,
  'type' => 'article',
  'title' => 'Create node using Node storage create method',
  'uid' => 1,
  'status' => TRUE,
];
$node = \Drupal::entityTypeManager()->getStorage($entity_type)->create($values);
$node->save();
DamienMcKenna’s picture

I wonder would this work?

$entity_type = 'node';
$values = [
  'nid' => NULL,
  'type' => 'article',
  'title' => 'Testing metatag creation',
  'uid' => 1,
  'status' => TRUE,
  'field_metatags' => [
    'x-default' => [
      'value' => serialize([
        'title' => 'Some title',
        'description' => 'Some description.',
      ]),
    ],
  ],
];
$node = \Drupal::entityTypeManager()->getStorage($entity_type)->create($values);
$node->save();
haynescw’s picture

I will give your last comment a try today.

haynescw’s picture

Well, given your example i found that all i needed to do was serialize the data, but you dont have to add it to x-default, you can just assign it to the field like so.

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

I was just able to confirm that using the following works as well.

$node->set('field_metatags', serialize([
'keywords' => 'Some Keywords',
'description' => 'Some Description',
]));

Where should I document this?

DamienMcKenna’s picture

@haynescw: Awesome, thanks for testing that and working out the correct structure.

@nixvan: Great, thanks!

Additions to the README.txt would be the best step, for now.

nicxvan’s picture

Status: Active » Needs review
FileSize
1.37 KB

Added some steps to the README.

DamienMcKenna’s picture

Documentation can go in 1.0.

DamienMcKenna’s picture

Some improvements.

DamienMcKenna’s picture

Status: Needs review » Fixed

Committed. Thanks, nicxvan!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

itsnadeem’s picture

please see this article for dynamically adding metatags.