You are here

protected function UserRole::findEntity in Feeds 8.3

Searches for an entity by entity key.

Parameters

string $field: The subfield to search in.

string $search: The value to search for.

Return value

int|bool The entity id, or false, if not found.

Overrides ConfigEntityReference::findEntity

File

src/Feeds/Target/UserRole.php, line 105

Class

UserRole
Defines a user role mapper.

Namespace

Drupal\feeds\Feeds\Target

Code

protected function findEntity(string $field, $search) {
  $entity_id = parent::findEntity($field, $search);
  if ($entity_id !== FALSE) {

    // Check if the role may be assigned.
    if (isset($this->configuration['allowed_roles'][$entity_id]) && !$this->configuration['allowed_roles'][$entity_id]) {

      // This role may *not* be assigned.
      throw new TargetValidationException($this
        ->t('The role %role may not be referenced.', [
        '%role' => $entity_id,
      ]));
    }
    return $entity_id;
  }

  // Automatically create a new role.
  if ($this->configuration['autocreate'] && in_array($this->configuration['reference_by'], [
    'id',
    'label',
  ])) {
    return $this
      ->createRole($search);
  }
}