public function GroupContentStorage::createForEntityInGroup in Group 2.0.x
Same name and namespace in other branches
- 8 src/Entity/Storage/GroupContentStorage.php \Drupal\group\Entity\Storage\GroupContentStorage::createForEntityInGroup()
Creates a GroupContent entity for placing a content entity in a group.
Parameters
\Drupal\Core\Entity\ContentEntityInterface $entity: The content entity to add to the group.
\Drupal\group\Entity\GroupInterface $group: The group to add the content entity to.
string $plugin_id: The ID of the group relation plugin to add the entity with.
array $values: (optional) Extra values to add to the GroupContent entity.
Return value
\Drupal\group\Entity\GroupContentInterface A new GroupContent entity.
Overrides GroupContentStorageInterface::createForEntityInGroup
File
- src/
Entity/ Storage/ GroupContentStorage.php, line 28
Class
- GroupContentStorage
- Defines the storage handler class for group content entities.
Namespace
Drupal\group\Entity\StorageCode
public function createForEntityInGroup(ContentEntityInterface $entity, GroupInterface $group, $plugin_id, $values = []) {
// An unsaved entity cannot have any group content.
if ($entity
->id() === NULL) {
throw new EntityStorageException("Cannot add an unsaved entity to a group.");
}
// An unsaved group cannot have any content.
if ($group
->id() === NULL) {
throw new EntityStorageException("Cannot add an entity to an unsaved group.");
}
// Check whether the entity can actually be added to the group.
$plugin = $group
->getGroupType()
->getContentPlugin($plugin_id);
if ($entity
->getEntityTypeId() != $plugin
->getEntityTypeId()) {
throw new EntityStorageException("Invalid plugin provided for adding the entity to the group.");
}
// Verify the bundle as well if the plugin is specific about them.
$supported_bundle = $plugin
->getEntityBundle();
if ($supported_bundle !== FALSE) {
if ($entity
->bundle() != $supported_bundle) {
throw new EntityStorageException("The provided plugin provided does not support the entity's bundle.");
}
}
// Set the necessary keys for a valid GroupContent entity.
$keys = [
'type' => $plugin
->getContentTypeConfigId(),
'gid' => $group
->id(),
'entity_id' => $entity
->id(),
];
// Return an unsaved GroupContent entity.
return $this
->create($keys + $values);
}