Paragraphs Editor
TypeUtility.php
Go to the documentation of this file.
1 <?php
2 
4 
11 
12 /**
13  * Contains helpers for strict type validation.
14  */
15 class TypeUtility {
16 
17  /**
18  * Enforces that an entity is a paragraph entity.
19  *
20  * @return \Drupal\paragraphs\ParagraphInterface|null
21  * The filtered entity.
22  */
23  public static function ensureParagraph(EntityInterface $entity = NULL) {
24  if (!$entity instanceof ParagraphInterface) {
25  throw new \Exception('Not a paragraph.');
26  }
27  return $entity;
28  }
29 
30  /**
31  * Enforces that an entity is a field config entity.
32  *
33  * @param \Drupal\Core\Field\FieldDefinitionInterface|null $field_definition
34  * The field definition to ensure is a field config instance.
35  *
36  * @return \Drupal\Core\Field\FieldConfigInterface|null
37  * The config object.
38  */
39  public static function ensureFieldConfig(FieldDefinitionInterface $field_definition = NULL) {
40  if (!$field_definition instanceof FieldConfigInterface) {
41  throw new \Exception('Not a field config.');
42  }
43  return $field_definition;
44  }
45 
46  /**
47  * Enforces that a field item list is an entity reference revisions instance.
48  *
49  * @param \Drupal\Core\Field\FieldItemListInterface $items
50  * The items to validate.
51  *
52  * @return \Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList
53  * The entity reference revisions object.
54  */
55  public static function ensureEntityReferenceRevisions(FieldItemListInterface $items) {
56  if (!$items instanceof EntityReferenceRevisionsFieldItemList) {
57  throw new \Exception('Not an entity reference revisions field.');
58  }
59  return $items;
60  }
61 
62 }
static ensureEntityReferenceRevisions(FieldItemListInterface $items)
Definition: TypeUtility.php:55
static ensureParagraph(EntityInterface $entity=NULL)
Definition: TypeUtility.php:23
static ensureFieldConfig(FieldDefinitionInterface $field_definition=NULL)
Definition: TypeUtility.php:39