You are here

public function KeyRepository::getKeyNamesAsOptions in Key 8

Get an array of key names, useful as options in form fields.

Parameters

array $filters: An array of filters to apply to the list of options.

Return value

array An array of key names, indexed by id.

Overrides KeyRepositoryInterface::getKeyNamesAsOptions

File

src/KeyRepository.php, line 121

Class

KeyRepository
Provides a repository for Key configuration entities.

Namespace

Drupal\key

Code

public function getKeyNamesAsOptions(array $filters = []) {
  $options = [];
  $keys = $this
    ->getKeys();
  foreach ($filters as $index => $filter) {
    switch ($index) {
      case 'type':
        $keys = array_intersect_key($this
          ->getKeysByType($filter), $keys);
        break;
      case 'provider':
        $keys = array_intersect_key($this
          ->getKeysByProvider($filter), $keys);
        break;
      case 'type_group':
        $keys = array_intersect_key($this
          ->getKeysByTypeGroup($filter), $keys);
        break;
      case 'storage_method':
        $keys = array_intersect_key($this
          ->getKeysByStorageMethod($filter), $keys);
        break;
    }
  }
  foreach ($keys as $key) {
    $key_id = $key
      ->id();
    $key_title = $key
      ->label();
    $options[$key_id] = (string) $key_title;
  }
  return $options;
}