You are here

ResourceFieldEntityText.php in RESTful 7.2

File

src/Plugin/resource/Field/ResourceFieldEntityText.php
View source
<?php

/**
 * @file
 * Contains \Drupal\restful\Plugin\resource\Field\ResourceFieldEntityText.
 */
namespace Drupal\restful\Plugin\resource\Field;

use Drupal\restful\Http\RequestInterface;
use Drupal\restful\Plugin\resource\DataInterpreter\DataInterpreterInterface;
class ResourceFieldEntityText extends ResourceFieldEntity implements ResourceFieldEntityInterface {

  /**
   * {@inheritdoc}
   */
  public function preprocess($value) {

    // Text field. Check if field has an input format.
    $field_info = field_info_field($this
      ->getProperty());

    // If there was no bundle that had the field instance, then return NULL.
    if (!($instance = field_info_instance($this
      ->getEntityType(), $this
      ->getProperty(), $this
      ->getBundle()))) {
      return NULL;
    }
    $return = NULL;
    if ($field_info['cardinality'] == 1) {

      // Single value.
      if (!$instance['settings']['text_processing']) {
        return $value;
      }
      return array(
        'value' => $value,
        // TODO: This is hardcoded! Fix it.
        'format' => 'filtered_html',
      );
    }

    // Multiple values.
    foreach ($value as $delta => $single_value) {
      if (!$instance['settings']['text_processing']) {
        $return[$delta] = $single_value;
      }
      else {
        $return[$delta] = array(
          'value' => $single_value,
          'format' => 'filtered_html',
        );
      }
    }
    return $return;
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function setRequest(RequestInterface $request) {
    $this->decorated
      ->setRequest($request);
  }

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

}

Classes