Hi,

I want to create a PHP script to create basic pages on Drupal 8. I need some advices, i don't know how to start, i just have my database connection. I don't know which data i have to create etc... Maybe i have to create a row in node...

Any idea ?

Comments

slewazimuth’s picture

Basic sections:

1. Setup error handling.
2. Initialize Drupal 8 before booting.
3. Boot the Drupal kernal.
4. Login.
5. Create a node entity with desired bundle.

Shashwat Purav’s picture

Thank You,
Shashwat Purav

gouli’s picture

This is very useful for Drupal beginners.

cofaure’s picture

In fact, i'm just trying to update the drupal database to create a page. So i need to know which tables i have to update (node__body etc)

Shashwat Purav’s picture

Best way to do it is, not by updating databse tables but using Drupal's API. Please find below useful link to create nodes programatically in D8.

Programmatically create nodes

Thank You,
Shashwat Purav

Jaypan’s picture

Shashwat is correct. You should never directly write to any tables that you have not created yourself. Always use the Drupal API for that.

cofaure’s picture

Ok guys i understand, i'll try with something like that :

use Drupal\node\Entity\Node;
$node = Node::create([
  'type' => 'article',
  'langcode' => 'en',
  'created' => REQUEST_TIME,
  'changed' => REQUEST_TIME,
  'uid' => 1,
  'title' => 'My test!',
  'field_tags' =>[2],
  'body' => [
    'summary' => '',
    'value' => '<p>The body of my node.</p>',
    'format' => 'full_html',
  ],
]);
$node->save();

Thank you

cofaure’s picture

Last question, i dont understand this line "use Drupal\node\Entity\Node;"

I have got a multisite drupal installation, which path i have to use ?

I don't know where i have to put the php code.

Jaypan’s picture

That's the namespace of Node. It tells the system both where the file is (for autoloaders), and ensures that it can be used even if you have another class named Node somewhere else in your system (since they will be in a different namespace).

This is related to the code base, not the database, so you will use the same value regardless of which site you are using in your multisite.

cofaure’s picture

OK thanks !

AmolB’s picture

Very nice post, thank you.