Skip to content

Instantly share code, notes, and snippets.

@dreambubbler
dreambubbler / ExampleUpdateUser.php
Created April 12, 2017 22:19
Drupal 8: Update user
<?php
use Drupal\user\Entity\User;
// Updating a user is a three step process:
// 1) load the user object to change
// 2) set property/field to new value
// 3) Save the user object.
// This example updates:
// 1) password
@dreambubbler
dreambubbler / ExampleAttachTermsProgrammatically.php
Last active May 9, 2020 13:05
Drupal 8: Attach terms to entity programmatically
<?php
use Drupal\node\Entity\Node;
/**
* Before attaching a term(s) to a term reference field,
* Must know:
* - field_example_name: the full name of the term reference field
* - tid: the term ID(s) to attach
*
* Keep in mind that this example uses Node::load()
@dreambubbler
dreambubbler / ExampleCreateTermProgrammatically.php
Last active April 6, 2020 22:50
Drupal 8 Create Taxonomy Term programmatically
$new_term = \Drupal\taxonomy\Entity\Term::create([
'vid' => 'example_vocabulary_machine_name',
'name' => 'Example term name',
]);
$new_term->enforceIsNew();
$new_term->save();