You are here

function _bricks_nest_get_field_item in Bricks​ 2.x

Helper to get the field item list a render array is made from.

Parameters

$content: A render array to view an entity.

Return value

The bricks field item.

1 call to _bricks_nest_get_field_item()
_bricks_nested_get_new_elements in ./bricks.module

File

./bricks.module, line 148

Code

function _bricks_nest_get_field_item(array $content) : ?BricksFieldItemInterface {
  $entity = NULL;

  // The default is the same #theme and entity type id, see
  // EntityViewBuilder::getBuildDefaults().
  if ($theme = $content['#theme'] ?? '') {
    $entity = $content["#{$theme}"] ?? NULL;
  }
  if (!$entity) {

    // If that didn't work out, try to fish for it among the properties. If
    // there is only one entity sure it is the one. If there is more than one,
    // give up.
    foreach (Element::properties($content) as $property) {
      if ($content[$property] instanceof EntityInterface) {
        if ($entity) {
          throw new \LogicException(sprintf('Unsupported render array with entity types %s %s', $entity
            ->getEntityTypeId(), $content[$property]
            ->getEntityTypeId()));
        }
        $entity = $content[$property];
      }
    }
  }
  return $entity ? $entity->_referringItem : NULL;
}