Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

$default = $entity->isDefaultRevision($new);

$children = $this->entityTypeManager
 $entity->setNewRevision($value);


->getStorage('taxonomy_term')
 $revision_id = $entity->getRevisionId();
->loadChildren($tid);
$tree = $this->entityTypeManager

->getStorage('taxonomy_term')
 ENTITY FIELDS
Content Entity
 ->loadTree($vid, 0, NULL, TRUE);
Entity Query \Drup…\Core\Entity\FieldableEntityInterface
$vocabulary = $term->getVocabularyId();
$term_name = $term->getName(); \Drupal\Core\Field\FieldItemInterface
$description = $term->getDescription();
ENTITY, NODE, TERM, USER \Drupal\Core\Field\FieldItemListInterface
$sort = $term->getWeight();
\Drupal\Core\TypedData\TypedDataInterface
\Drupal\Core\Access\AccessibleInterface $body_text = $node->body->value;
\Drupal\user\UserInterface
\D…\Core\Cache\CacheableDependencyInterface $body_text = $node->get('body')->value;
\Drupal\user\Entity\User
$body_array = $node->body->getValue();
\Drupal\Core\Entity\ContentEntityBase $user = User::load($uid); P $body_text example: '<p>Hello!</p>'
\Drupal\Core\Entity\ContentEntityInterface $is_admin = $user->hasRole($role_id);
$body_array example: array(
$is_active = $user->isActive();
\Drupal\Core\Entity\EntityInterface
$first_name = $user->field_name->value; 0 => array(
Content entities: 'aggregator_feed', 'value' => '<p>Hello!</p>',
'aggregator_item', 'block_content', 'comment', \Drupal\file\FileInterface 'summary' => '',
'contact_message', 'file', 'menu_link_content', 'format' => 'basic_html',
\Drupal\file\Entity\File 'promote', 'sticky', 'revision_timestamp', 'revision_uid',
'menu_link_content', 'node', 'shortcut', ))
$image = File::load($fid); P
$field_item_list = $entity
 'revision_log', 'path', 'body', 'comment', 'field_image',
'taxonomy_term', 'user'. $name = $file->getFilename() 'field_tags'.
->get('field_name');
$uri = $file->getFileUri();
$entity = Entity::create($values); P $mime_type = $file->getMimeType(); $fields_array = $entity->toArray();
$entity = Entity::load($id); P $size = $file->getSize(); $field_item_lists = $entity->getFields(); $field_properties = array_keys($node

$entities = Entity::loadMultiple($ids); P $temp_file = $file->isTemporary(); $fields_array Array of raw values per field. ->getFieldDefinition('body')

->getFieldStorageDefinition()

$entity->save(); $field_item_lists Array of FieldItemList per field.
$entity->delete(); ->getPropertyDefinitions());
$text_format = $node->body->format;
ENTITY TRANSLATION $third_tag_id = $node->field_tags[2]
 Text plain field properties: 'value'.
$entity_id = $entity->id(); ->target_id; Text formatted field properties: 'value', 'format',
$bundle = $entity->bundle(); \Dru…\Core\TypedData\TranslatableInterface 'processed'.
$not_saved = $entity->isNew(); $has_body = $node->hasField('body');
$can_edit = $entity->access('edit', $language = $entity->language(); Text with summary field properties: 'value', 'format',
$empty_body = $node->body->isEmpty();
$account); $translatable = $entity->isTranslatable(); $fields = $node->getTranslatableFields(); 'processed', 'summary', 'summary_processed'.
$default = $entity->isDefaultTranslation(); $entity->set('title', 'New title'); Taxonomy entity reference field properties:
$in_dutch = $entity->hasTranslation('nl');
\Drupal\node\NodeInterface addTranslation($langcode, $values); 'target_id', 'entity'.
$field_names = array_keys($node

\Drupal\node\Entity\Node $dutch_content = $entity
 File entity reference field properties: 'target_id',
->getFields());
->getTranslation('nl'); $field_names = array_keys($this
 'entity', 'display', 'description'.
\Drupal\user\EntityOwnerInterface
->entityFieldManager
 Image entity reference field properties: 'target_id',
$node = $this->entityTypeManager
 ->getFieldDefinitions('node', 'article'));
->getStorage('node')
 ENTITY REVISIONS 'entity', 'display', 'description', 'alt', 'title', 'width',
$definition = $entity

->load($nid); ->getFieldDefinition('field_name'); 'height'.
$node = Node::load($nid); P \Drupal\Core\Entity\RevisionableInterface Date field properties: 'value', 'date'.
$node->save(); Node field names: 'nid', 'uuid', 'vid', 'type',
$node->delete(); $is_new = $entity->isNewRevision(); 'langcode', 'title', 'uid', 'status', 'created', 'changed',
ENTITY REFERENCE FIELDS
$node_type = $node->getType();
$title = $node->getTitle(); $vid = $node->field_tags->entity

$visible = $node->isPublished(); EntityTypeManager EntityStorage ContentEntity ->getVocabularyId();
$node->setPromoted(TRUE); $entity = $container->get('entity_type.manager')->getStorage('entity_type')->load($entity_id); $uri = $node->field_file->entity

$uid = $node->getOwnerId(); ->getFileUri();
$author = $node->getOwner();
ContentEntity FieldItemList string/integer
$name = $node->getOwner()->getDisplayName();
Drupal\taxonomy\TermInterface $field_value = $node->field_name->value; $roles = $node->getOwner()->getRoles();

Drupal\taxonomy\Entity\Term ContentEntity FieldItemList TypedData


$term = $this->entityTypeManager

$field_value = $node->get('field_name')->get(0)->get('value')->getValue();
->getStorage('taxonomy_term')

->load($tid); FieldItem string/integer
$term = Term::load($tid); P

January 2016 Compatible with Drupal 8.0 www.wizzlern.nl/drupal/cheat-sheets


ENTITY STORAGE ENTITY ACCESS ENTITY QUERY: RANGE, SORT, PAGER

\Drupal\Core\Entity\EntityStorageInterface \Drupal\Core\Access\AccessibleInterface $name_taken = (bool) $this->entityQuery



->get('user')

\D…\Core\Entity\Sql\SqlContentEntityStorage hook_entity_*() ->condition('name', $name)

hook_entity_*() hook_ENTITY_TYPE_*() ->range(0, 1)

->count()

* = create, load, insert, update, view, * = access, create_access, field_access. ->execute();
view_alter, view_mode_alter, $entity->access($operation);
view_display_alter, prepare_view, $entity->field_name->access($operation); $sorted_terms = $this->entityQuery

presave, predelete, delete. ->get('taxonomy_term')

Entity access operations: view, create, update, delete. ->sort('weight')

hook_ENTITY_TYPE_*() Entities can define additional operations. ->sort('name')

* = create, load, insert, update, view, Entity field_access operations: view, edit. ->addTag('term_access')

view_alter, presave, predelete, delete. ->execute();
$this->entityStorage = $container
 ENTITY QUERY $entity_query = $this->queryFactory

->get('entity_type.manager')
 ->get('user')

->getStorage('entity_type'); ->condition('uid', 0, '<>')

$entity_storage = \Drupal::
 \Drupal\Core\Config\Entity\Query\Query
->pager(50);
entityTypeManager()
 \Drupal\Core\Entity\Query\QueryFactory $header = $this->buildHeader();
->getStorage('entity_type'); P $entity_query->tableSort($header);
\Drupal\Core\Entity\Query\QueryInterface
$uids = $entity_query->execute();
$entity = $this->entityStorage
 $this->entityQuery = $container
 DOCUMENTATION
->load($entity_id); ->get('entity.query');
$entities = $this->entityStorage
 SERVICES Entity API docs: api.drupal.org
->loadMultiple($entity_ids); $query = $this->entityQuery
 Community documentation: Entity API - Drupal 8
$revision = $this->entityStorage
 ->getAggregate('node');
->loadRevision($revision_id); $query =\Drupal::
 entity.query Hooks: file entity.api.php
entityQueryAggregate('node'); P entity.repository
$entity = $this->entityStorage

entity_display.repository P = Procedural code. Do not use this in OO code
->create($entity_values); $node_ids = $this->entityQuery->get('node')
 because it can not be unit tested or replaced.
$this->entityStorage->delete($entities); ->condition('type', 'article')
 entity_field.manager
$this->entityStorage->save($entity); ->condition('status', 1)

entity_type.bundle.info
->execute();
entity_type.manager
ENTITY VIEW $query = $this->entityQuery
 entity_type.repository

->get('aggregator_feed');
\Dr…\Core\Entity\EntityViewBuilderInterface $or_condition = $query->orConditionGroup()

->condition('title', $feed->label())

\Drupal\Core\Entity\EntityViewBuilder ->condition('url', $feed->getUrl());
$this->viewBuilder = $this
 $duplicates = $query

->entityTypeManager
 ->condition($or_condition)

->getViewBuilder('node'); ->execute();
$view_builder = \Drupal::entityTypeManager()

->getViewBuilder('node'); P Condition operators (3rd param):
 P
$build = $view_builder

->view($node, 'teaser');
 '=', '<>', '>', '>=', '<', '<=',
'STARTS_WITH', 'CONTAINS',
'ENDS_WITH', 'IN', 'NOT IN',
'BETWEEN'.

QueryFactory Query array

$query = $container->get('entity.query')->get('content_type')->execute();

EntityTypeManager EntityViewBuilder (render) array


$build = $this->entityTypeManager->getViewBuilder('node')->view($node, 'teaser');

January 2016 Compatible with Drupal 8.0 www.wizzlern.nl/drupal/cheat-sheets

You might also like