I have code which creates an artice from JSON.

$article = $node_storage->create([
      'type' => 'article',
      'title' => $json['title'],
	  'field_int100' => $json['int100'],
	  'field_text100' => $json['text100'],
      'body' => $json['body'],
    ]);
    $article->save();

By default, the text format is Plain Text. Modifying the code which specifies the value of the body field will import the text with HTML formatting.

'body' => [
    'value' => $json['body'],
    'format' => 'full_html'
    ],

I got this code  from an AI query. How can I find the property statements for other properties?  E.g IsPublished, Author details. I guess this information comes from a class somewhere in Drupal core but so far I can't find the file. Can anyone help?  Thanks.

Comments

jaypan’s picture

The "properties" are field names on the entity type. The code you showed is creating an entity of type node, with a bundle type of article. There are three types of fields on an entity:

  • Base fields - these are defined in code. They are defined on the entity type, and shared across all bundles of that entity type. For example, the type field on node is used to store the type of node, and is required on all nodes, and is defined in \Drupal\node\Entity\Node::baseFieldDefinitions. The field IDs are the keys of the array returned from that function. You can find base fields for other entity types in the same manner.
  • Fields added through the UI (or by config, but this is less common). These are dynamic fields added to a specific system to customize it. Most entity types in Drupal are fieldable, and will have a UI page somewhere allowing developers to set up fields for the entity type. Most of these fields will be prefixed with field_, eg field_user_picture, although sometimes they will not be, for example body
  • Computed fields. These are computed at runtime, and are added through code, but they can be harder to find. They are also somewhat lesser used, so you'll find almost all fields will be one of the first two types.

Contact me to contract me for D7 -> D10/11 migrations.

peterk900’s picture

The link \Drupal\node\Entity\Node::baseFieldDefinitions was useful.

I managed to find two code snippets which give me the machine names for the properties I'm after, including entity and field level.

<?php

use Drupal\node\Entity\Node;
use Drupal\Core\Entity\EntityStorageException;

/**
 * Creates a node and promotes it to the front page.
 */
function mymodule_create_promoted_node() {
  try {
    // Create a new node object
    $node = Node::create([
      'type'        => 'article', // Change to your content type machine name
      'title'       => 'My Programmatically Created Node',
      'body'        => [
        'value'  => '<p>This is the body text of the node.</p>',
        'format' => 'full_html',
      ],
      'status'      => 1, // 1 = Published, 0 = Unpublished
      'promote'     => 1, // 1 = Promoted to front page
      'sticky'      => 0, // Optional: 1 = Sticky at top of lists
      'uid'         => 1, // Author user ID
    ]);

    // Save the node
    $node->save();

    \Drupal::logger('mymodule')->notice('Node @nid created and promoted.', [
      '@nid' => $node->id(),
    ]);

    return $node->id();

  } catch (EntityStorageException $e) {
    \Drupal::logger('mymodule')->error('Failed to create node: @msg', [
      '@msg' => $e->getMessage(),
    ]);
    return NULL;
  }
}


public function setUnpublished() {
  $key = $this->getEntityType()
    ->getKey('published');
  $this->set($key, FALSE);
  return $this;
}

I still can't find the core code which specifies these keys ( published, status, sticky etc.). But I think I have all the values which apply for nodes.