You are here

public function jDrupalResource::get in jDrupal 8.0.x

File

src/Plugin/rest/resource/jDrupalResource.php, line 92

Class

jDrupalResource
Provides a resource to get bundles by entity.

Namespace

Drupal\jDrupal\Plugin\rest\resource

Code

public function get($entity = NULL) {
  if ($entity) {
    $permission = 'Administer content types';
    if (!$this->currentUser
      ->hasPermission($permission)) {
      throw new AccessDeniedHttpException();
    }
    $bundles_entities = \Drupal::entityManager()
      ->getStorage($entity . '_type')
      ->loadMultiple();
    $bundles = array();
    foreach ($bundles_entities as $entity) {
      $bundles[$entity
        ->id()] = $entity
        ->label();
    }
    if (!empty($bundles)) {
      return new ResourceResponse($bundles);
    }
    throw new NotFoundHttpException(t('Bundles for entity @entity were not found', array(
      '@entity' => $entity,
    )));
  }
  throw new HttpException(t('Entity wasn\'t provided'));
}