You are here

class ResourceField in RESTful 7.2

Hierarchy

Expanded class hierarchy of ResourceField

1 file declares its use of ResourceField
LoginCookie__1_0.php in src/Plugin/resource/LoginCookie__1_0.php
Contains \Drupal\restful\Plugin\resource\LoginCookie__1_0.

File

src/Plugin/resource/Field/ResourceField.php, line 15
Contains \Drupal\restful\Plugin\resource\ResourceField.

Namespace

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

  /**
   * Constructor.
   *
   * @param array $field
   *   Contains the field values.
   *
   * @throws ServerConfigurationException
   */
  public function __construct(array $field, RequestInterface $request) {
    $this
      ->setRequest($request);
    if (empty($field['public_name'])) {
      throw new ServerConfigurationException('No public name provided in the field mappings.');
    }
    $this->publicName = $field['public_name'];
    $this->accessCallbacks = isset($field['access_callbacks']) ? $field['access_callbacks'] : $this->accessCallbacks;
    $this->property = isset($field['property']) ? $field['property'] : $this->property;

    // $this->column = isset($field['column']) ? $field['column'] : $this->column;
    $this->callback = isset($field['callback']) ? $field['callback'] : $this->callback;
    $this->processCallbacks = isset($field['process_callbacks']) ? $field['process_callbacks'] : $this->processCallbacks;
    $this->resource = isset($field['resource']) ? $field['resource'] : $this->resource;
    $this->methods = isset($field['methods']) ? $field['methods'] : $this->methods;

    // Store the definition, useful to access custom keys on custom resource
    // fields.
    $this->definition = $field;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(array $field, RequestInterface $request = NULL) {
    $request = $request ?: restful()
      ->getRequest();
    if ($class_name = static::fieldClassName($field)) {
      if ($class_name != get_called_class() && $class_name != '\\' . get_called_class()) {

        // Call the create factory in the derived class.
        return call_user_func_array(array(
          $class_name,
          'create',
        ), array(
          $field,
          $request,
          new static($field, $request),
        ));
      }
    }

    // If no other class was found, then use the current one.
    $resource_field = new static($field, $request);
    $resource_field
      ->addDefaults();
    return $resource_field;
  }

  /**
   * {@inheritdoc}
   */
  public function value(DataInterpreterInterface $interpreter) {
    if ($callback = $this
      ->getCallback()) {
      return ResourceManager::executeCallback($callback, array(
        $interpreter,
      ));
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function set($value, DataInterpreterInterface $interpreter) {

    // ResourceField only supports callbacks, so no set is possible.
  }

  /**
   * {@inheritdoc}
   */
  public function access($op, DataInterpreterInterface $interpreter) {
    foreach ($this
      ->getAccessCallbacks() as $callback) {
      $result = ResourceManager::executeCallback($callback, array(
        $op,
        $this,
        $interpreter,
      ));
      if ($result == ResourceFieldBase::ACCESS_DENY) {
        return FALSE;
      }
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function addDefaults() {

    // Almost all the defaults come are applied by the object's property
    // defaults.
    if (!($resource = $this
      ->getResource())) {
      return;
    }

    // Expand array to be verbose.
    if (!is_array($resource)) {
      $resource = array(
        'name' => $resource,
      );
    }

    // Set default value.
    $resource += array(
      'fullView' => TRUE,
    );

    // Set the default value for the version of the referenced resource.
    if (!isset($resource['majorVersion']) || !isset($resource['minorVersion'])) {
      list($major_version, $minor_version) = restful()
        ->getResourceManager()
        ->getResourceLastVersion($resource['name']);
      $resource['majorVersion'] = $major_version;
      $resource['minorVersion'] = $minor_version;
    }
    $this
      ->setResource($resource);
  }

  /**
   * Get the class name to use based on the field definition.
   *
   * @param array $field_definition
   *   The processed field definition with the user values.
   *
   * @return string
   *   The class name to use. If the class name is empty or does not implement
   *   ResourceFieldInterface then ResourceField will be used. NULL if nothing
   *   was found.
   */
  public static function fieldClassName(array $field_definition) {
    if (!empty($field_definition['class'])) {
      $class_name = $field_definition['class'];
    }
    elseif (!empty($field_definition['sub_property']) || !empty($field_definition['formatter']) || !empty($field_definition['wrapper_method']) || !empty($field_definition['wrapper_method_on_entity']) || !empty($field_definition['column']) || !empty($field_definition['image_styles']) || (!empty($field_definition['property']) ? field_info_field($field_definition['property']) : NULL)) {
      $class_name = '\\Drupal\\restful\\Plugin\\resource\\Field\\ResourceFieldEntity';
    }
    elseif (!empty($field_definition['property'])) {
      $class_name = '\\Drupal\\restful\\Plugin\\resource\\Field\\ResourceFieldKeyValue';
    }
    if (!empty($class_name) && class_exists($class_name) && in_array('Drupal\\restful\\Plugin\\resource\\Field\\ResourceFieldInterface', class_implements($class_name))) {
      return $class_name;
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function compoundDocumentId(DataInterpreterInterface $interpreter) {

    // Since this kind of field can be anything, just return the value.
    return $this
      ->value($interpreter);
  }

  /**
   * {@inheritdoc}
   */
  public function render(DataInterpreterInterface $interpreter) {
    return $this
      ->executeProcessCallbacks($this
      ->value($interpreter));
  }

  /**
   * {@inheritdoc}
   */
  public function getCardinality() {

    // Default to cardinality of 1.
    return 1;
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
ResourceField::access public function Check access on property by the defined access callbacks. Overrides ResourceFieldInterface::access
ResourceField::addDefaults public function Adds the default values to the definitions array. Overrides ResourceFieldInterface::addDefaults
ResourceField::compoundDocumentId public function Fetches the embedded identifier(s) for the current resource field, if any. Overrides ResourceFieldInterface::compoundDocumentId 1
ResourceField::create public static function Factory. Overrides ResourceFieldInterface::create 2
ResourceField::fieldClassName public static function Get the class name to use based on the field definition.
ResourceField::getCardinality public function Gets the cardinality of the wrapped field. Overrides ResourceFieldInterface::getCardinality
ResourceField::render public function Gets the value of a field and applies all process callbacks to it. Overrides ResourceFieldInterface::render
ResourceField::set public function Gets the value for the field given a data source. Overrides ResourceFieldInterface::set
ResourceField::setCardinality public function Set the cardinality. Overrides ResourceFieldInterface::setCardinality
ResourceField::value public function Gets the value for the field given a data source. Overrides ResourceFieldInterface::value 2
ResourceField::__construct public function Constructor. 1
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