/** * {@inheritdoc} */ public function getProperties($include_computed = FALSE) { if (!isset($this->entity)) { throw new MissingDataException('Unable to get properties as no entity has been provided.'); } if (!$this->entity instanceof FieldableEntityInterface) { // @todo: Add support for config entities in // https://www.drupal.org/node/1818574. return array(); } return $this->entity->getFields($include_computed); }
function getHostConfiguration(EntityInterface $entity, $prefix = 'field_ran_') { // TODO: This can't be right... there must be an easier way to access a // field collection's fields. $field_collection = $entity->getFields()['field_ran_host_configuration']->getIterator()[0]; $fields = $field_collection->getFieldCollectionItem()->getFields(); $vars = array(); $len = strlen($prefix); foreach ($fields as $name => $field) { if (substr($name, 0, $len) == $prefix) { $vars[substr($name, $len)] = array(); foreach ($field->getIterator() as $object) { $vars[substr($name, $len)][] = $object->getString(); } $vars[substr($name, $len)] = array_filter($vars[substr($name, $len)]); } } return $vars; }
/** * Responds to entity GET requests. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity object. * * @return \Drupal\rest\ResourceResponse * The response containing the entity with its accessible fields. * * @throws \Symfony\Component\HttpKernel\Exception\HttpException */ public function get(EntityInterface $entity) { if (!$entity->access('view')) { throw new AccessDeniedHttpException(); } $fields = $entity->getFields(); $display_id = $entity->getEntityTypeId() . '.' . $entity->bundle() . '.restx'; // Remove hidden fields (Display mode 'default'). $view_display = \Drupal::entityManager()->getStorage('entity_view_display')->load($display_id); if ($view_display) { $content = $view_display->get('content'); foreach ($fields as $field_name => $field) { if (!$field->access('view') || substr($field_name, 0, 6) === 'field_' && !isset($content[$field_name])) { unset($fields[$field_name]); } } } $output = array('data' => $fields); $response = new ResourceResponse($output, ResourceResponse::HTTP_OK); $response->addCacheableDependency($output); return $response; }