diff --git a/group.module b/group.module index 046b178..31a4088 100644 --- a/group.module +++ b/group.module @@ -8,6 +8,8 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; @@ -191,6 +193,60 @@ function group_form_block_form_alter(&$form, FormStateInterface $form_state, $fo } /** + * Implements hook_entity_base_field_info(). + * + * Creates computed fields to get the groups related to a entity. There will be + * a field containing all groups. Each group type will also get a separate + * field. + * + * We currently only use this for nodes and users since they can be installed + * as group content. + */ +function group_entity_base_field_info(EntityTypeInterface $entity_type) { + $fields = []; + + $entity_types = ['node', 'user']; + if (in_array($entity_type->id(), $entity_types)) { + $fields['groups'] = BaseFieldDefinition::create('entity_reference') + ->setName('groups') + ->setTargetEntityTypeId($entity_type->id()) + ->setSetting('target_type', 'group') + ->setLabel(t('Get the related groups for this entity.')) + ->setDescription(t('A list of all the groups referencing this entity.')) + ->setTranslatable(FALSE) + ->setComputed(TRUE) + ->setClass('\Drupal\group\Fields\GroupGroupReferenceItemList') + ->setDisplayConfigurable('view', TRUE) + ->setDisplayOptions('view', [ + 'label' => 'above', + 'weight' => -5, + ]); + + foreach (\Drupal::service('entity_type.manager')->getStorage('group_type')->loadMultiple() as $group_type) { + /** @var \Drupal\group\Entity\GroupType $group_type */ + $field_name = 'groups_type_' . $group_type->id(); + $fields[$field_name] = BaseFieldDefinition::create('entity_reference') + ->setName($field_name) + ->setTargetEntityTypeId($entity_type->id()) + ->setSetting('target_type', 'group') + ->setSetting('handler_settings', ['target_bundles' => [$group_type->id() => $group_type->id()]]) + ->setLabel(t('Get the related @type groups for this entity.', ['@type' => $group_type->label()])) + ->setDescription(t('A list of all the groups referencing this entity.')) + ->setTranslatable(FALSE) + ->setComputed(TRUE) + ->setClass('\Drupal\group\Fields\GroupGroupReferenceItemList') + ->setDisplayConfigurable('view', TRUE) + ->setDisplayOptions('view', [ + 'label' => 'above', + 'weight' => -5, + ]); + } + } + + return $fields; +} + +/** * Implements hook_entity_delete(). */ function group_entity_delete(EntityInterface $entity) { diff --git a/src/Fields/GroupGroupReferenceItemList.php b/src/Fields/GroupGroupReferenceItemList.php new file mode 100644 index 0000000..c602435 --- /dev/null +++ b/src/Fields/GroupGroupReferenceItemList.php @@ -0,0 +1,113 @@ +entityTypeManager = \Drupal::service('entity_type.manager'); + } + + /** + * {@inheritdoc} + */ + public function get($index) { + $this->computeValues(); + return isset($this->list[$index]) ? $this->list[$index] : NULL; + } + + /** + * {@inheritdoc} + */ + public function referencedEntities() { + $this->computeValues(); + return parent::referencedEntities(); + } + + /** + * {@inheritdoc} + */ + public function getIterator() { + $this->computeValues(); + return parent::getIterator(); + } + + /** + * {@inheritdoc} + */ + public function computeValues() { + // We only support nodes and users. + if ($this->getEntity()->getEntityTypeId() === 'node') { + $plugin_id = 'group_node:' . $this->getEntity()->bundle(); + } + elseif ($this->getEntity()->getEntityTypeId() === 'user') { + $plugin_id = 'group_membership'; + } + else { + return NULL; + } + + // No value will exist if the entity has not been created so exit early.:wq + if ($this->getEntity()->isNew()) { + return NULL; + } + + $handler_settings = $this->getItemDefinition()->getSetting('handler_settings'); + $group_types = isset($handler_settings['target_bundles']) ? $handler_settings['target_bundles'] : $this->entityTypeManager->getStorage('group_type')->loadMultiple(); + + $group_content_types = $this->entityTypeManager->getStorage('group_content_type')->loadByProperties([ + 'group_type' => array_keys($group_types), + 'content_plugin' => $plugin_id, + ]); + + if (empty($group_content_types)) { + return NULL; + } + + $group_contents = $this->entityTypeManager->getStorage('group_content')->loadByProperties([ + 'type' => array_keys($group_content_types), + 'entity_id' => $this->getEntity()->id(), + ]); + + $this->list = []; + if (!empty($group_contents)) { + foreach ($group_contents as $delta => $group_content) { + $this->list[] = $this->createItem($delta, [ + 'target_id' => $group_content->gid->target_id, + ]); + } + } + } + +}