View Drupal 8 : Static db query
<?php /* Main */
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
$autoloader = require_once 'autoload.php';
$kernel = new DrupalKernel('prod', $autoloader);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
View Drupal 8 : Render view programmatically
<?php
$id = 'a';
$view = \Drupal\views\Views::getView('glossary');
$view->setDisplay('default');
$view->setArguments([$id]);
$view->execute();
$results = $view->result;
foreach($results as $result){
$nid = $result->nid;
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
View Drupal 8: Query all contents with title startwith a letter
$content = '<ul>';
$first_char = 'a';
$query = \Drupal::entityQuery('node');
$query->condition('title', '%'.$first_char.'%','LIKE');
$query->range(0,25);
$nids = $query->execute();
$node_storage = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($nids);
foreach ($node_storage as $node){
$content = $content.'<li>'.$node->title->value.'</li>';
View Drupal 8: Get current URI
$current_uri = \Drupal::request()->getRequestUri();
View Delete all content entities in Drupal 8
<?php
// Delete all nodes.
entity_delete_multiple('node', \Drupal::entityQuery('node')->execute());
// Delete all files.
entity_delete_multiple('file', \Drupal::entityQuery('file')->execute());
// Delete all taxonomy terms.
entity_delete_multiple('taxonomy_term', \Drupal::entityQuery('taxonomy_term')->execute());
View Drupal 8: Attach terms to entity programmatically.php
<?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()
View Drupal 8 - Create term programatically
<?php /* Main */
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
$autoloader = require_once 'autoload.php';
$kernel = new DrupalKernel('prod', $autoloader);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
View Drupal 8 - Load term by name programtically
<?php
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
$autoloader = require_once 'autoload.php';
$kernel = new DrupalKernel('prod', $autoloader);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
View __INDEX.txt
# 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:
View update-d8-node-save
use Drupal\node\Entity\Node;
function _svv_tools_set_author() {
$user = user_load_by_name('test');
$query = \Drupal::entityQuery('node');
$nids = $query->execute();
dd($nids);
foreach ($nids as $nid) {
$node = Node::load($nid);
$node->uid->value = $user->id();