nortmas
9/27/2017 - 3:56 PM

Entity basics

<?php

use Drupal\Core\Access\AccessibleInterface;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;

$entity = Entity::load($id);
$entities = Entity::loadMultiple($ids);

# Get entity type
$entity_type = $entity->getEntityTypeId();
# Get node type
$bundle = $entity->bundle();
# Get id
$entity_id = $entity->id();
# Get link
$link = $entity->toLink($entity->title->value)->toString();

$entity = Entity::create($values);
$entity->setTitle('MY NEW TITLE'); // This is a special meta field
$entity->set('FIELD_NAME', 'THIS IS DATA'); // This is a Field added in to the content type
$entity->save();
$entity->delete();

$not_saved = $entity->isNew();
$can_edit = $entity->access('edit', $account);
$entity = $container->get('entity_type.manager')->getStorage('entity_type')->load($entity_id);
use Drupal\taxonomy\TermInterface;
use Drupal\taxonomy\Entity\Term;

$term = $this->entityTypeManager->getStorage('taxonomy_term')->load($tid);
$term = Term::load($tid);
$children = $this->entityTypeManager->getStorage('taxonomy_term')->loadChildren($tid);
$tree = $this->entityTypeManager->getStorage('taxonomy_term')->loadTree($vid, 0, NULL, TRUE);
$vocabulary = $term->getVocabularyId();
$term_name = $term->getName();
$description = $term->getDescription();
$sort = $term->getWeight(); 
use Drupal\node\NodeInterface;
use Drupal\node\Entity\Node;
use Drupal\user\EntityOwnerInterface;

$node = $this->entityTypeManager->getStorage('node')->load($nid);
$node = Node::load($nid);
$node->save();
$node->delete();
$node_type = $node->getType();
$title = $node->getTitle();
$visible = $node->isPublished();
$node->setPromoted(TRUE);
$uid = $node->getOwnerId();
$author = $node->getOwner(); 

$node->get('field_name')->getString();
// Use this:
$val = $node->get('field_name')->getValue();
check_markup($val[0]['value'], $val[0]['format']);
use Drupal\config_pages\Entity\ConfigPages;

# Config pages module  - Get a config page by machine_name
$configurations = ConfigPages::config('machine_name');
use Drupal\file\FileInterface;
use Drupal\file\Entity\File;

$image = File::load($fid);
$name = $file->getFilename()
$uri = $file->getFileUri();
$mime_type = $file->getMimeType();
$size = $file->getSize();
$temp_file = $file->isTemporary(); 
use Drupal\Core\TypedData\TranslatableInterface;

$language = $entity->language();
$translatable = $entity->isTranslatable();
$default = $entity->isDefaultTranslation();
$in_dutch = $entity->hasTranslation('nl');
addTranslation($langcode, $values);
$dutch_content = $entity->getTranslation('nl'); 
use Drupal\user\UserInterface;
use Drupal\user\Entity\User;

$user = User::load($uid);
$is_admin = $user->hasRole($role_id);
$is_active = $user->isActive();
$first_name = $user->field_name->value;