You are here

public static function Og::getSelectionHandler in Organic groups 8

Get the selection handler for an audience field attached to entity.

@codingStandardsIgnoreStart @codingStandardsIgnoreEnd

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field definition.

array $options: Overriding the default options of the selection handler.

Return value

\Drupal\og\Plugin\EntityReferenceSelection\OgSelection Returns the OG selection handler.

Throws

\Exception Thrown when the passed in field definition is not of a group audience field.

Deprecated

in og:8.x-1.0-alpha4 and is removed from og:8.x-1.0-alpha5. Use \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager::getInstance() Instead.

See also

https://github.com/Gizra/og/issues/580

File

src/Og.php, line 398

Class

Og
A static helper class for OG.

Namespace

Drupal\og

Code

public static function getSelectionHandler(FieldDefinitionInterface $field_definition, array $options = []) {

  // @codingStandardsIgnoreStart
  @trigger_error('Og:getSelectionHandler() is deprecated in og:8.x-1.0-alpha4
      and is removed from og:8.x-1.0-alpha5.
      Use \\Drupal\\Core\\Entity\\EntityReferenceSelection\\SelectionPluginManager::getInstance()
      instead. See https://github.com/Gizra/og/issues/580', E_USER_DEPRECATED);

  // @codingStandardsIgnoreEnd
  if (!\Drupal::service('og.group_audience_helper')
    ->isGroupAudienceField($field_definition)) {
    $field_name = $field_definition
      ->getName();
    throw new \Exception("The field {$field_name} is not an audience field.");
  }
  $default_options = [
    'target_type' => $field_definition
      ->getFieldStorageDefinition()
      ->getSetting('target_type'),
    'handler' => $field_definition
      ->getSetting('handler'),
    'field_mode' => 'default',
  ] + $field_definition
    ->getSetting('handler_settings');

  // Override with passed $options.
  $options = NestedArray::mergeDeep($default_options, $options);
  return \Drupal::service('plugin.manager.entity_reference_selection')
    ->createInstance('og:default', $options);
}