Skip to content

EntityHelper

Provides helpers for working with entities.


  • Full name: \Drupal\helper\EntityHelper

Properties

pathValidator

protected \Drupal\Core\Path\PathValidatorInterface $pathValidator

Methods

__construct

Constructs an EntityHelper.

public __construct(\Drupal\Core\Path\PathValidatorInterface $pathValidator): mixed

Parameters:

Parameter Type Description
$pathValidator \Drupal\Core\Path\PathValidatorInterface
The path validator.

matchesPath

Checks to see if the entity matches a given path.

public matchesPath(\Drupal\Core\Entity\EntityInterface $entity, string $path, string|null $rel = null, array $options = []): bool

Note, this does not validate that the user can access the path.

Parameters:

Parameter Type Description
$entity \Drupal\Core\Entity\EntityInterface
The entity.
$path string
The path to check.
$rel string|null
The parameter to pass as the first argument to EntityInterface::toUrl().
$options array
The parameter to pass as the second argument to EntityInterface::toUrl().

Return Value:

TRUE if the entity matches the path, FALSE otherwise.


hasValue

Checks to if an entity's field is available and is not empty.

public static hasValue(\Drupal\Core\Entity\EntityInterface $entity, string $fieldName): bool
  • This method is static. Parameters:
Parameter Type Description
$entity \Drupal\Core\Entity\EntityInterface
The entity.
$fieldName string
The field name.

Return Value:

TRUE if the field is not empty, FALSE otherwise.


getState

Determines the state of an entity.

public static getState(\Drupal\Core\Entity\EntityInterface $entity): string|\Drupal\Core\StringTranslation\TranslatableMarkup

This looks at the following conditions: - If the entity is in the trash - If the entity is moderated (returns the moderated state label) - If the entity is published or not - If the entity is config, if it is enabled or not.

  • This method is static. Parameters:
Parameter Type Description
$entity \Drupal\Core\Entity\EntityInterface
The entity.

Return Value:

The state of the entity. Workflow state labels are returned as strings. All other states are returned as translatable markup.


toArray

Generate an array of data about an entity.

public static toArray(\Drupal\Core\Entity\EntityInterface $entity, array $urlOptions = ['absolute' => true]): array
  • This method is static. Parameters:
Parameter Type Description
$entity \Drupal\Core\Entity\EntityInterface
The entity.
$urlOptions array
(optional) An array of options to pass whenever generating a URL.

Return Value:

The generated entity row data.