Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Drupal 8 - Examples
# Taxonomy terms:
- https://gist.github.com/facine/35bb291811c146b6fc9e#file-create_taxonomy_term-php
# Menu links:
- https://gist.github.com/facine/35bb291811c146b6fc9e#file-create_menu_link-php
# File items:
- https://gist.github.com/facine/35bb291811c146b6fc9e#file-create_file-php
# Nodes:
# Simple node:
- https://gist.github.com/facine/35bb291811c146b6fc9e#file-create_node-php
# Node with image field:
- https://gist.github.com/facine/35bb291811c146b6fc9e#file-create_node_with_image-php
<?php
// Programmatically create files.
use Drupal\file\Entity\File;
$file = File::create([
'uid' => 1,
'filename' => 'logo.svg',
'uri' => 'public://page/logo.svg',
'status' => 1,
]);
$file->save();
<?php
// Programmatically create and translate nodes.
use Drupal\node\Entity\Node;
$node = Node::create([
// The node entity bundle.
'type' => 'article',
'langcode' => 'en',
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
// The user ID.
'uid' => 1,
'title' => 'My test!',
// An array with taxonomy terms.
'field_tags' =>[2],
'body' => [
'summary' => '',
'value' => '<p>The body of my node.</p>',
'format' => 'full_html',
],
]);
$node->save();
\Drupal::service('path.alias_storage')->save("/node/" . $node->id(), "/my/path", "en");
$node_es = $node->addTranslation('es');
$node_es->title = 'Mi prueba!';
$node_es->body->value = '<p>El cuerpo de mi nodo.</p>';
$node_es->body->format = 'full_html';
$node_es->save();
\Drupal::service('path.alias_storage')->save("/node/" . $node->id(), "/mi/ruta", "es");
<?php
// Programmatically create node with image fields.
use Drupal\file\Entity\File;
use Drupal\node\Entity\Node;
$file = File::create([
'uid' => 1,
'uri' => 'public://page/logo.png',
'status' => 1,
]);
$file->save();
$node = Node::create([
'type' => 'article',
'langcode' => 'en',
'created' => REQUEST_TIME,
'changed' => REQUEST_TIME,
'uid' => 1,
'title' => 'My title',
'field_tags' =>[2],
'body' => [
'summary' => '',
'value' => 'My node!',
'format' => 'full_html',
],
'field_images' => [
[
'target_id' => $file->id(),
'alt' => "My 'alt'",
'title' => "My 'title'",
],
],
]);
$node->save();
\Drupal::service('path.alias_storage')->save('/node/' . $node->id(), '/my-path', 'en');
<?php
// Programmatically create and translate taxonomy terms.
use Drupal\taxonomy\Entity\Term;
$term = Term::create([
'vid' => 'sport_activity',
'langcode' => 'en',
'name' => 'My tag',
'description' => [
'value' => '<p>My description.</p>',
'format' => 'full_html',
],
'weight' => -1
'parent' => array (0);
]);
$term->save();
\Drupal::service('path.alias_storage')->save("/taxonomy/term/" . $term->id(), "/tags/my-tag", "en");
$term_es = $term->addTranslation('es');
$term_es->name = 'Mi etiqueta';
$term_es->description->value = '<p>Mi descripción.</p>';
$term_es->description->format = 'full_html';
$term_es->save();
\Drupal::service('path.alias_storage')->save("/taxonomy/term/" . $term->id(), "/etiquetas/mi-etiqueta", "es");
@chadhumphrey

This comment has been minimized.

Copy link

@chadhumphrey chadhumphrey commented May 26, 2016

Thanks for the gist. I'm new to Drupal 8 (not php), but I copied create_node.php into a file and tried to run it. I'm getting the following error.

Fatal error: Uncaught Error: Class 'Drupal\node\Entity\Node' not found in /var/www/html/drupal/web/load_it.php:6 Stack trace: #0 {main} thrown in /var/www/html/drupal/web/load_it.php on line 6

Line 6:
$node = Node::create([

The function I want to access (create) seems to be in core/lib/Drupal/Core/Entity/entity.php

So I'm confused, thanks.

@isholgueras

This comment has been minimized.

Copy link

@isholgueras isholgueras commented Jun 2, 2016

Thanks for these snippets, @facine!

@keopx

This comment has been minimized.

Copy link

@keopx keopx commented Jun 3, 2016

@facine I love you :-*

@gautiermichelin

This comment has been minimized.

Copy link

@gautiermichelin gautiermichelin commented Jun 18, 2017

Hi, small errors in create_taxonomy_term.php :

(...)
  'weight' => -1,
  'parent' => array (0)
(...)

Many thanks :-)

@blakemorgan

This comment has been minimized.

Copy link

@blakemorgan blakemorgan commented Jun 30, 2017

Great work! Thanks for the help. Just so you know, as of Drupal 8.3.x, REQUEST_TIME has been deprecated. They ask use now to use \Drupal::time()->getRequestTime().

@alvar0hurtad0

This comment has been minimized.

Copy link

@alvar0hurtad0 alvar0hurtad0 commented Jul 22, 2017

FACINE
<3

@ron-garcia

This comment has been minimized.

Copy link

@ron-garcia ron-garcia commented Dec 14, 2018

How do I add a taxonomy term to an image?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment