You are here

abstract class ResourceFieldBase in RESTful 7.2

Hierarchy

Expanded class hierarchy of ResourceFieldBase

5 files declare their use of ResourceFieldBase
Articles__1_5.php in modules/restful_example/src/Plugin/resource/node/article/v1/Articles__1_5.php
Contains \Drupal\restful_example\Plugin\resource\node\article\v1\Articles__1_5.
Formatter.php in src/Plugin/formatter/Formatter.php
Contains \Drupal\restful\Plugin\formatter\Formatter
FormatterJsonApi.php in src/Plugin/formatter/FormatterJsonApi.php
Contains \Drupal\restful\Plugin\formatter\FormatterJsonApi.
FormatterSingleJson.php in src/Plugin/formatter/FormatterSingleJson.php
Contains \Drupal\restful\Plugin\formatter\FormatterSingleJson.
Main__1_1.php in tests/modules/restful_test/src/Plugin/resource/entity_test/main/v1/Main__1_1.php
Contains \Drupal\restful_test\Plugin\resource\entity_test\main\v1\Main__1_1.

File

src/Plugin/resource/Field/ResourceFieldBase.php, line 18
Contains \Drupal\restful\Plugin\resource\Field\ResourceFieldBase.

Namespace

Drupal\restful\Plugin\resource\Field
View source
abstract class ResourceFieldBase implements ResourceFieldInterface {

  /**
   * Return this value from public field access callbacks to allow access.
   */
  const ACCESS_ALLOW = 'allow';

  /**
   * Return this value from public field access callbacks to deny access.
   */
  const ACCESS_DENY = 'deny';

  /**
   * Return this value from public field access callbacks to not affect access.
   */
  const ACCESS_IGNORE = NULL;

  /**
   * Contains the public field name.
   */
  protected $publicName;

  /**
   * An array of callbacks to determine if user has access to the property. Note
   * that this callback is on top of the access provided by entity API, and is
   * used for convenience, where for example write operation on a property
   * should be denied only on certain request conditions. The Passed arguments
   * are:
   *   - op: The operation that access should be checked for. Can be "view" or
   *     "edit".
   *   - public_field_name: The name of the public field.
   *   - property_wrapper: The wrapped property.
   *   - wrapper: The wrapped entity.
   *
   * @var array
   */
  protected $accessCallbacks = array();

  /**
   * The entity property (e.g. "title", "nid").
   *
   * @var string
   */
  protected $property;

  /**
   * A callable callback to get a computed value. The wrapped entity is passed
   * as argument. Defaults To FALSE. The callback function receive as first
   * argument the entity.
   *
   * @var mixed
   */
  protected $callback;

  /**
   * An array of callbacks to perform on the returned value, or an array with
   * the object and method.
   *
   * @var array
   */
  protected $processCallbacks = array();

  /**
   * This property can be assigned only to an entity reference field. Array of
   * restful resources keyed by the target bundle. For example, if the field is
   * referencing a node entity, with "Article" and "Page" bundles, we are able
   * to map those bundles to their related resource. Items with bundles that
   * were not explicitly set would be ignored.
   *
   * It is also possible to pass an array as the value, with:
   *   - "name": The resource name.
   *   - "fullView": Determines if the referenced resource should be rendered,
   *   or just the referenced ID(s) to appear. Defaults to TRUE.
   *   array(
   *     // Shorthand.
   *     'article' => 'articles',
   *     // Verbose
   *     'page' => array(
   *       'name' => 'pages',
   *       'fullView' => FALSE,
   *     ),
   *   );
   *
   * @var array
   */
  protected $resource = array();

  /**
   * A generic array storage.
   *
   * @var array
   */
  protected $metadata = array();

  /**
   * The HTTP methods where this field applies.
   *
   * This replaces the create_or_update_passthrough feature. Defaults to all.
   *
   * @var array
   */
  protected $methods = array(
    RequestInterface::METHOD_GET,
    RequestInterface::METHOD_HEAD,
    RequestInterface::METHOD_POST,
    RequestInterface::METHOD_PUT,
    RequestInterface::METHOD_PATCH,
    RequestInterface::METHOD_OPTIONS,
  );

  /**
   * The request object to be used.
   *
   * @var RequestInterface
   */
  protected $request;

  /**
   * The field definition array.
   *
   * Use with caution.
   *
   * @var array
   */
  protected $definition = array();

  /**
   * Information about the field.
   *
   * @var PublicFieldInfoInterface
   */
  protected $publicFieldInfo;

  /**
   * Holds the field cardinality.
   *
   * @var int
   */
  protected $cardinality;

  /**
   * Get the request in the data provider.
   *
   * @return RequestInterface
   *   The request.
   */
  public function getRequest() {
    return $this->request;
  }

  /**
   * Set the request.
   *
   * @param RequestInterface $request
   *   The request.
   */
  public function setRequest(RequestInterface $request) {
    $this->request = $request;
  }

  /**
   * {@inheritdoc}
   */
  public function getPublicName() {
    return $this->publicName;
  }

  /**
   * {@inheritdoc}
   */
  public function setPublicName($public_name) {
    $this->publicName = $public_name;
  }

  /**
   * {@inheritdoc}
   */
  public function getAccessCallbacks() {
    return $this->accessCallbacks;
  }

  /**
   * {@inheritdoc}
   */
  public function setAccessCallbacks($access_callbacks) {
    $this->accessCallbacks = $access_callbacks;
  }

  /**
   * {@inheritdoc}
   */
  public function getProperty() {
    return $this->property;
  }

  /**
   * {@inheritdoc}
   */
  public function setProperty($property) {
    $this->property = $property;
  }

  /**
   * {@inheritdoc}
   */
  public function getCallback() {
    return $this->callback;
  }

  /**
   * {@inheritdoc}
   */
  public function setCallback($callback) {
    $this->callback = $callback;
  }

  /**
   * {@inheritdoc}
   */
  public function getProcessCallbacks() {
    return $this->processCallbacks;
  }

  /**
   * {@inheritdoc}
   */
  public function setProcessCallbacks($process_callbacks) {
    $this->processCallbacks = $process_callbacks;
  }

  /**
   * {@inheritdoc}
   */
  public function getResource() {
    return $this->resource;
  }

  /**
   * {@inheritdoc}
   */
  public function setResource($resource) {
    $this->resource = $resource;
  }

  /**
   * {@inheritdoc}
   */
  public function getMethods() {
    return $this->methods;
  }

  /**
   * {@inheritdoc}
   */
  public function setMethods($methods) {
    foreach ($methods as $method) {
      if (Request::isValidMethod($method)) {
        throw new ServerConfigurationException(sprintf('The method %s in the field resource mapping is not valid.', $method));
      }
    }
    $this->methods = $methods;
  }

  /**
   * {@inheritdoc}
   */
  public function id() {
    return $this->publicName;
  }

  /**
   * {@inheritdoc}
   */
  public function isComputed() {
    return !$this
      ->getProperty();
  }

  /**
   * {@inheritdoc}
   */
  public static final function isArrayNumeric(array $input) {
    $keys = array_keys($input);
    foreach ($keys as $key) {
      if (!ctype_digit((string) $key)) {
        return FALSE;
      }
    }
    return isset($keys[0]) ? $keys[0] == 0 : TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function addMetadata($key, $value) {
    $path = explode(':', $key);
    $leave = array_pop($path);
    $element =& $this
      ->internalMetadataElement($key);
    $element[$leave] = $value;
  }

  /**
   * {@inheritdoc}
   */
  public function getMetadata($key) {
    $path = explode(':', $key);
    $leave = array_pop($path);
    $element = $this
      ->internalMetadataElement($key);
    return isset($element[$leave]) ? $element[$leave] : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefinition() {
    return $this->definition;
  }

  /**
   * {@inheritdoc}
   */
  public function executeProcessCallbacks($value) {
    $process_callbacks = $this
      ->getProcessCallbacks();
    if (!isset($value) || empty($process_callbacks)) {
      return $value;
    }
    foreach ($process_callbacks as $process_callback) {
      $value = ResourceManager::executeCallback($process_callback, array(
        $value,
      ));
    }
    return $value;
  }

  /**
   * Returns the last array element from the nested namespace array.
   *
   * Searches in the metadata nested array the element in the data tree pointed
   * by the colon separated key. If the key goes through a non-existing path, it
   * initalize an empty array. The reference to that element is returned for
   * reading and writing purposes.
   *
   * @param string $key
   *   The namespaced key.
   *
   * @return array
   *   The reference to the array element.
   */
  protected function &internalMetadataElement($key) {

    // If there is a namespace, then use it to do nested arrays.
    $path = explode(':', $key);
    array_pop($path);
    $element =& $this->metadata;
    foreach ($path as $path_item) {
      if (!isset($element[$path_item])) {

        // Initialize an empty namespace.
        $element[$path_item] = array();
      }
      $element = $element[$path_item];
    }
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function getPublicFieldInfo() {
    return $this->publicFieldInfo;
  }

  /**
   * {@inheritdoc}
   */
  public function setPublicFieldInfo(PublicFieldInfoInterface $public_field_info) {
    $this->publicFieldInfo = $public_field_info;
  }

  /**
   * Basic auto discovery information.
   *
   * @return array
   *   The array of information ready to be encoded.
   */
  public function autoDiscovery() {
    return $this
      ->getPublicFieldInfo()
      ->prepare();
  }

  /**
   * Returns the basic discovery information for a given field.
   *
   * @param string $name
   *   The name of the public field.
   *
   * @return array
   *   The array of information ready to be encoded.
   */
  public static function emptyDiscoveryInfo($name) {
    $info = new PublicFieldInfoNull($name);
    return $info
      ->prepare();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ResourceFieldBase::$accessCallbacks protected property An array of callbacks to determine if user has access to the property. Note that this callback is on top of the access provided by entity API, and is used for convenience, where for example write operation on a property should be denied only on…
ResourceFieldBase::$callback protected property A callable callback to get a computed value. The wrapped entity is passed as argument. Defaults To FALSE. The callback function receive as first argument the entity.
ResourceFieldBase::$cardinality protected property Holds the field cardinality.
ResourceFieldBase::$definition protected property The field definition array.
ResourceFieldBase::$metadata protected property A generic array storage.
ResourceFieldBase::$methods protected property The HTTP methods where this field applies.
ResourceFieldBase::$processCallbacks protected property An array of callbacks to perform on the returned value, or an array with the object and method.
ResourceFieldBase::$property protected property The entity property (e.g. "title", "nid").
ResourceFieldBase::$publicFieldInfo protected property Information about the field.
ResourceFieldBase::$publicName protected property Contains the public field name.
ResourceFieldBase::$request protected property The request object to be used.
ResourceFieldBase::$resource protected property This property can be assigned only to an entity reference field. Array of restful resources keyed by the target bundle. For example, if the field is referencing a node entity, with "Article" and "Page" bundles, we are able to map…
ResourceFieldBase::ACCESS_ALLOW constant Return this value from public field access callbacks to allow access.
ResourceFieldBase::ACCESS_DENY constant Return this value from public field access callbacks to deny access.
ResourceFieldBase::ACCESS_IGNORE constant Return this value from public field access callbacks to not affect access.
ResourceFieldBase::addMetadata public function Add metadata to the field. Overrides ResourceFieldInterface::addMetadata
ResourceFieldBase::autoDiscovery public function Basic auto discovery information.
ResourceFieldBase::emptyDiscoveryInfo public static function Returns the basic discovery information for a given field.
ResourceFieldBase::executeProcessCallbacks public function Executes the process callbacks. Overrides ResourceFieldInterface::executeProcessCallbacks
ResourceFieldBase::getAccessCallbacks public function Overrides ResourceFieldInterface::getAccessCallbacks
ResourceFieldBase::getCallback public function Overrides ResourceFieldInterface::getCallback
ResourceFieldBase::getDefinition public function Gets the original field definition as declared in Resource::publicFields(). Overrides ResourceFieldInterface::getDefinition
ResourceFieldBase::getMetadata public function Add metadata to the field. Overrides ResourceFieldInterface::getMetadata
ResourceFieldBase::getMethods public function Overrides ResourceFieldInterface::getMethods
ResourceFieldBase::getProcessCallbacks public function Overrides ResourceFieldInterface::getProcessCallbacks
ResourceFieldBase::getProperty public function Overrides ResourceFieldInterface::getProperty
ResourceFieldBase::getPublicFieldInfo public function Gets the public field info object. Overrides ResourceFieldInterface::getPublicFieldInfo
ResourceFieldBase::getPublicName public function Overrides ResourceFieldInterface::getPublicName
ResourceFieldBase::getRequest public function Get the request in the data provider. Overrides ResourceFieldInterface::getRequest
ResourceFieldBase::getResource public function Overrides ResourceFieldInterface::getResource
ResourceFieldBase::id public function Gets the ID of the resource field. Overrides ResourceFieldInterface::id
ResourceFieldBase::internalMetadataElement protected function Returns the last array element from the nested namespace array.
ResourceFieldBase::isArrayNumeric final public static function Helper method to determine if an array is numeric. Overrides ResourceFieldInterface::isArrayNumeric
ResourceFieldBase::isComputed public function Checks if the current field is computed. Overrides ResourceFieldInterface::isComputed
ResourceFieldBase::setAccessCallbacks public function Overrides ResourceFieldInterface::setAccessCallbacks
ResourceFieldBase::setCallback public function Overrides ResourceFieldInterface::setCallback
ResourceFieldBase::setMethods public function Overrides ResourceFieldInterface::setMethods
ResourceFieldBase::setProcessCallbacks public function Overrides ResourceFieldInterface::setProcessCallbacks
ResourceFieldBase::setProperty public function Overrides ResourceFieldInterface::setProperty
ResourceFieldBase::setPublicFieldInfo public function Gets the public field info object. Overrides ResourceFieldInterface::setPublicFieldInfo
ResourceFieldBase::setPublicName public function Overrides ResourceFieldInterface::setPublicName
ResourceFieldBase::setRequest public function Set the request. Overrides ResourceFieldInterface::setRequest
ResourceFieldBase::setResource public function Overrides ResourceFieldInterface::setResource
ResourceFieldInterface::access public function Check access on property by the defined access callbacks. 3
ResourceFieldInterface::addDefaults public function Adds the default values to the definitions array. 3
ResourceFieldInterface::compoundDocumentId public function Fetches the embedded identifier(s) for the current resource field, if any. 3
ResourceFieldInterface::create public static function Factory. 3
ResourceFieldInterface::getCardinality public function Gets the cardinality of the wrapped field. 3
ResourceFieldInterface::render public function Gets the value of a field and applies all process callbacks to it. 3
ResourceFieldInterface::set public function Gets the value for the field given a data source. 3
ResourceFieldInterface::setCardinality public function Set the cardinality. 3
ResourceFieldInterface::value public function Gets the value for the field given a data source. 3