diff --git a/src/Entity/FieldCollectionItem.php b/src/Entity/FieldCollectionItem.php index 5fc7d51..9205c3e 100644 --- a/src/Entity/FieldCollectionItem.php +++ b/src/Entity/FieldCollectionItem.php @@ -383,14 +383,11 @@ class FieldCollectionItem extends ContentEntityBase implements FieldCollectionIt } /** - * Gets the entity type id of the host. - * - * @return string - * The host entity type id. + * {@inheritdoc} */ - protected function getHostType() { - $field = $this->getFieldDefinition($this->bundle()); - return $field->getTargetEntityTypeId(); + public function getHostTypeId() { + $host_type_id = $this->host_type->value; + return !empty($host_type_id) ? $host_type_id : NULL; } } diff --git a/src/FieldCollectionItemAccessControlHandler.php b/src/FieldCollectionItemAccessControlHandler.php index 2644df6..0e73182 100644 --- a/src/FieldCollectionItemAccessControlHandler.php +++ b/src/FieldCollectionItemAccessControlHandler.php @@ -35,11 +35,20 @@ class FieldCollectionItemAccessControlHandler extends EntityAccessControlHandler if ($result->isForbidden()) { return $result; } - /** @var \Drupal\Core\Entity\ContentEntityInterface $host */ + /** @var \Drupal\field_collection\FieldCollectionItemInterface $entity */ if ($host = $entity->getHost()) { + /** @var \Drupal\Core\Entity\ContentEntityInterface $host */ return $host->access($operation, $account, TRUE); } else { + // Check if the host type has been initialized. When a new host entity is + // added it doesn't have yet an id but the field collection knows already + // the host entity type. In this case allow the access. + // @see \Drupal\field_collection\Entity\FieldCollectionItem::setHostEntity() + if (!empty($entity->getHostTypeId())) { + return AccessResult::allowed(); + } + // Don't allow access to orphan field_collection_item entities. // @todo Log error when no host found. return AccessResult::forbidden(); } diff --git a/src/FieldCollectionItemInterface.php b/src/FieldCollectionItemInterface.php index e04ac14..fbd3134 100644 --- a/src/FieldCollectionItemInterface.php +++ b/src/FieldCollectionItemInterface.php @@ -67,4 +67,12 @@ interface FieldCollectionItemInterface extends ContentEntityInterface { */ public function isEmpty(); + /** + * Gets the entity type id of the host. + * + * @return string|null + * The host entity type id or NULL if it has not been initialized. + */ + public function getHostTypeId(); + }