Score:0

Provide menu link when creating node programmatically and append it to parent menu link

in flag

I am creating a Node programatically:

Node::create([
  'title' => "Some name",
  'type' => "people",
  'body' => $params['body'],
  'field_people_degree' => $params['field_people_degree'],
  'field_people_email' => $params['field_people_email'],
  'field_field_image' => ['target_id' => $this->createImageFile($params['field_field_image'], $params['field_pub_tool_id'])->id()],
  'field_people_first_name' => $params['field_people_first_name'],
  'field_people_last_name' => $params['field_people_last_name'],
  'field_people_phone' => $params['field_people_phone'],
  'field_people_position' => $params['field_people_position'],
]);

and now I want to provide a menu link and append it under certain parents about-us>people>scientists. enter image description here

I tried this:

  MenuLinkContent::create([
    'title' => 'Some name',
    'link' => ['uri' => 'entity:about-us/people/scientists/some-name'],
    'menu_name' => 'main',
    'weight' => 0,
  ])->save();

but it crashes the DB. Any help?

ru flag
`entity:about-us/people/scientists/some-name` - this cannot work, maybe you mean `entity:ENTITY_TYPE/ENTITY_ID`. For URL schemas see e.g. [Url::fromUri](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/function/Url%3A%3AfromUri/8.6.x)
in flag
@Hudri thanks for your comment. So is entity type in this case `people`? And entity id the id of the created node?
cn flag
Entity type is node, entity ID is the node ID
in flag
@Clive could you help me with how I then set a custom URL alias and put the menu link under the following parents: - about-us - people - scientists ?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.