You are here

class OgGroupAudienceHelper in Organic groups 8

OG audience field helper methods.

Hierarchy

Expanded class hierarchy of OgGroupAudienceHelper

1 string reference to 'OgGroupAudienceHelper'
og.services.yml in ./og.services.yml
og.services.yml
1 service uses OgGroupAudienceHelper
og.group_audience_helper in ./og.services.yml
Drupal\og\OgGroupAudienceHelper

File

src/OgGroupAudienceHelper.php, line 15

Namespace

Drupal\og
View source
class OgGroupAudienceHelper implements OgGroupAudienceHelperInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * Constructs an OgGroupAudienceHelper object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager) {
    $this->entityTypeManager = $entity_type_manager;
    $this->entityFieldManager = $entity_field_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function hasGroupAudienceField($entity_type_id, $bundle_id) {
    return (bool) $this
      ->getAllGroupAudienceFields($entity_type_id, $bundle_id);
  }

  /**
   * {@inheritdoc}
   */
  public static function isGroupAudienceField(FieldDefinitionInterface $field_definition) {
    return $field_definition
      ->getType() == OgGroupAudienceHelperInterface::GROUP_REFERENCE;
  }

  /**
   * {@inheritdoc}
   */
  public function getAllGroupAudienceFields($group_content_entity_type_id, $group_content_bundle_id, $group_entity_type_id = NULL, $group_bundle_id = NULL) {
    $return = [];
    $entity_type = $this->entityTypeManager
      ->getDefinition($group_content_entity_type_id);
    if (!$entity_type
      ->entityClassImplements(FieldableEntityInterface::class)) {

      // This entity type is not fieldable.
      return [];
    }
    $field_definitions = $this->entityFieldManager
      ->getFieldDefinitions($group_content_entity_type_id, $group_content_bundle_id);
    foreach ($field_definitions as $field_definition) {
      if (!$this
        ->isGroupAudienceField($field_definition)) {

        // Not a group audience field.
        continue;
      }
      $target_type = $field_definition
        ->getFieldStorageDefinition()
        ->getSetting('target_type');
      if (isset($group_entity_type_id) && $target_type != $group_entity_type_id) {

        // Field doesn't reference this group type.
        continue;
      }
      $handler_settings = $field_definition
        ->getSetting('handler_settings');
      if (isset($group_bundle_id) && !empty($handler_settings['target_bundles']) && !in_array($group_bundle_id, $handler_settings['target_bundles'])) {
        continue;
      }
      $field_name = $field_definition
        ->getName();
      $return[$field_name] = $field_definition;
    }
    return $return;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OgGroupAudienceHelper::$entityFieldManager protected property The entity field manager.
OgGroupAudienceHelper::$entityTypeManager protected property The entity type manager.
OgGroupAudienceHelper::getAllGroupAudienceFields public function Returns all the group audience fields of a certain bundle. Overrides OgGroupAudienceHelperInterface::getAllGroupAudienceFields
OgGroupAudienceHelper::hasGroupAudienceField public function Returns whether the given entity bundle has a group audience field. Overrides OgGroupAudienceHelperInterface::hasGroupAudienceField
OgGroupAudienceHelper::isGroupAudienceField public static function Returns TRUE if field is a group audience type. Overrides OgGroupAudienceHelperInterface::isGroupAudienceField
OgGroupAudienceHelper::__construct public function Constructs an OgGroupAudienceHelper object.
OgGroupAudienceHelperInterface::DEFAULT_FIELD constant The default OG audience field name.
OgGroupAudienceHelperInterface::GROUP_REFERENCE constant The name of the field type that references non-user entities to groups.