/** * Custom callback to get the URL alias from an article. * * @param DataInterpreterInterface $interpreter * The data interpreter for the current article entity. * * @return string * The URL alias for the article. */ public function getUrlAlias(DataInterpreterInterface $interpreter) { /** @var \EntityDrupalWrapper $wrapper */ $wrapper = $interpreter->getWrapper(); $path = sprintf('node/%d', $wrapper->getIdentifier()); return drupal_get_path_alias($path); }
/** * {@inheritdoc} */ protected static function checkPropertyAccess(ResourceFieldInterface $resource_field, $op, DataInterpreterInterface $interpreter) { $term = $interpreter->getWrapper()->value(); if ($resource_field->getProperty() == 'name' && empty($term->tid) && $op == 'edit') { return TRUE; } return parent::checkPropertyAccess($resource_field, $op, $interpreter); }
/** * Process callback to generate the image styles for the current file. * * @param DataInterpreterInterface $interpreter * The data interpreter for the current article entity. * @param string[] $image_styles * Array of image style names. * * @return array * A list of URLs for this images' image styles. */ public function generateStyleUris(DataInterpreterInterface $interpreter, array $image_styles) { // Call image_style_url with the retrieved $value for each $image_style. $uri = $interpreter->getWrapper()->value()->uri; return array_map(function ($image_style) use($uri) { return url(image_style_url($image_style, $uri), array('absolute' => $this->isAbsolute)); }, $image_styles); }
/** * Returns the URL to the endpoint result. * * @param DataInterpreterInterface $interpreter * The plugin's data interpreter. * * @return string * The RESTful endpoint URL. */ public function getSelf(DataInterpreterInterface $interpreter) { if ($menu_item = $interpreter->getWrapper()->get('menuItem')) { $url = variable_get('restful_hook_menu_base_path', 'api') . '/' . $menu_item; return url($url, array('absolute' => TRUE)); } $base_path = variable_get('restful_hook_menu_base_path', 'api'); return url($base_path . '/v' . $interpreter->getWrapper()->get('majorVersion') . '.' . $interpreter->getWrapper()->get('minorVersion') . '/' . $interpreter->getWrapper()->get('resource'), array('absolute' => TRUE)); }
/** * An access callback that returns TRUE if title is "access". Otherwise FALSE. * * @param string $op * The operation that access should be checked for. Can be "view" or "edit". * Defaults to "edit". * @param ResourceFieldInterface $resource_field * The resource field to check access upon. * @param DataInterpreterInterface $interpreter * The data interpreter. * * @return string * "Allow" or "Deny" if user has access to the property. */ public static function publicFieldAccessFalse($op, ResourceFieldInterface $resource_field, DataInterpreterInterface $interpreter) { return $interpreter->getWrapper()->label() == 'access' ? \Drupal\restful\Plugin\resource\Field\ResourceFieldBase::ACCESS_ALLOW : \Drupal\restful\Plugin\resource\Field\ResourceFieldBase::ACCESS_DENY; }
/** * Get the wrapper for the property associated to the current field. * * @param DataInterpreterInterface $interpreter * The data source. * * @return \EntityMetadataWrapper * Either a \EntityStructureWrapper or a \EntityListWrapper. * * @throws ServerConfigurationException */ protected function propertyWrapper(DataInterpreterInterface $interpreter) { // This is the first method that gets called for all fields after loading // the entity. We'll use that opportunity to set the actual bundle of the // field. $this->setBundle($interpreter->getWrapper()->getBundle()); // Exposing an entity field. $wrapper = $interpreter->getWrapper(); // For entity fields the DataInterpreter needs to contain an EMW. if (!$wrapper instanceof \EntityDrupalWrapper) { throw new ServerConfigurationException('Cannot get a value without an entity metadata wrapper data source.'); } $property = $this->getProperty(); try { return $property && !$this->isWrapperMethodOnEntity() ? $wrapper->{$property} : $wrapper; } catch (\EntityMetadataWrapperException $e) { throw new UnprocessableEntityException(sprintf('The property %s could not be found in %s:%s.', $property, $wrapper->type(), $wrapper->getBundle())); } }
/** * Get the "self" url. * * @param DataInterpreterInterface $interpreter * The wrapped entity. * * @return string * The self URL. */ public function getEntitySelf(DataInterpreterInterface $interpreter) { return $this->versionedUrl($interpreter->getWrapper()->getIdentifier()); }