You are here

class PublicFieldInfoBase in RESTful 7.2

Hierarchy

Expanded class hierarchy of PublicFieldInfoBase

2 files declare their use of PublicFieldInfoBase
DataProvider.php in src/Plugin/resource/DataProvider/DataProvider.php
Contains \Drupal\restful\Plugin\resource\DataProvider\DataProvider.
ResourceFieldBase.php in src/Plugin/resource/Field/ResourceFieldBase.php
Contains \Drupal\restful\Plugin\resource\Field\ResourceFieldBase.

File

src/Plugin/resource/Field/PublicFieldInfo/PublicFieldInfoBase.php, line 12
Contains \Drupal\restful\Plugin\resource\Field\PublicFieldInfo\PublicFieldInfoBase.

Namespace

Drupal\restful\Plugin\resource\Field\PublicFieldInfo
View source
class PublicFieldInfoBase implements PublicFieldInfoInterface {

  /**
   * Default categories for the field information.
   *
   * @var array[]
   */
  protected static $defaultSections = array(
    'info' => array(
      'label' => '',
      'description' => '',
    ),
    // Describe the data.
    'data' => array(
      'type' => NULL,
      'read_only' => FALSE,
      'cardinality' => 1,
      'required' => FALSE,
    ),
    // Information about the form element.
    'form_element' => array(
      'type' => NULL,
      'default_value' => '',
      'placeholder' => '',
      'size' => NULL,
      'allowed_values' => NULL,
    ),
  );

  /**
   * Sections for the field information.
   *
   * @var array[]
   */
  protected $categories = array();

  /**
   * The name of the public field.
   *
   * @var string
   */
  protected $fieldName = '';

  /**
   * PublicFieldInfoBase constructor.
   *
   * @param string $field_name
   *   The name of the field.
   * @param array[] $sections
   *   The array of categories information.
   */
  public function __construct($field_name, array $sections = array()) {
    $this->fieldName = $field_name;
    $sections = drupal_array_merge_deep($this::$defaultSections, $sections);
    foreach ($sections as $section_name => $section_info) {
      $this
        ->addCategory($section_name, $section_info);
    }
  }

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

  /**
   * {@inheritdoc}
   */
  public function addCategory($category_name, array $section_info) {
    try {
      $this
        ->validate($category_name, $section_info);

      // Process the section info adding defaults if needed.
      $this->categories[$category_name] = $this
        ->process($category_name, $section_info);
    } catch (ServerConfigurationException $e) {

      // If there are validation errors do not add the section.
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getSection($section_name) {
    return empty($this->categories[$section_name]) ? array() : $this->categories[$section_name];
  }

  /**
   * {@inheritdoc}
   */
  public function addSectionDefaults($section_name, array $section_info) {
    $this
      ->addCategory($section_name, array_merge($section_info, $this
      ->getSection($section_name)));
  }

  /**
   * Validates the provided data for the section.
   *
   * @param string $section_name
   *   The name of the categories. By default RESTful supports 'info',
   *   'form_element' and 'data'.
   * @param array $section_info
   *   The structured array with the section information.
   *
   * @throws ServerConfigurationException
   *   If the field does not pass validation.
   */
  protected function validate($section_name, array $section_info) {
    if ($section_name == 'info') {
      $this
        ->validateInfo($section_info);
    }
    elseif ($section_name == 'data') {
      $this
        ->validateData($section_info);
    }
    elseif ($section_name == 'form_element') {
      $this
        ->validateFormElement($section_info);
    }
  }

  /**
   * Processes the provided data for the section.
   *
   * @param string $section_name
   *   The name of the categories. By default RESTful supports 'info',
   *   'form_element' and 'data'.
   * @param array $section_info
   *   The structured array with the section information.
   *
   * @returns array
   *   The processed section info.
   */
  protected function process($section_name, array $section_info) {
    if ($section_name == 'data') {
      if ($section_info['type'] == 'string') {
        $section_info['size'] = isset($section_info['size']) ? $section_info['size'] : 255;
      }
    }
    elseif ($section_name == 'form_element') {

      // Default title and description to the ones in the 'info' section.
      if (empty($section_info['title'])) {
        $section_info['title'] = empty($this->categories['info']['title']) ? $this->fieldName : $this->categories['info']['title'];
        if (!empty($this->categories['info']['description'])) {
          $section_info['description'] = $this->categories['info']['description'];
        }
      }
    }
    return $section_info;
  }

  /**
   * Validates the info section.
   *
   * @param array $section_info
   *   The structured array with the section information.
   *
   * @throws ServerConfigurationException
   *   If the field does not pass validation.
   */
  protected function validateInfo(array $section_info) {
    if (empty($section_info['label'])) {
      throw new ServerConfigurationException(sprintf('The label information is missing for this field: %s.', $this->fieldName));
    }
  }

  /**
   * Validates the info section.
   *
   * @param array $section_info
   *   The structured array with the section information.
   *
   * @throws ServerConfigurationException
   *   If the field does not pass validation.
   */
  protected function validateData(array $section_info) {
    if (empty($section_info['type'])) {
      throw new ServerConfigurationException(sprintf('The schema information is not valid for this field: %s.', $this->fieldName));
    }
  }

  /**
   * Validates the info section.
   *
   * @param array $section_info
   *   The structured array with the section information.
   *
   * @throws ServerConfigurationException
   *   If the field does not pass validation.
   */
  protected function validateFormElement(array $section_info) {
    if (empty($section_info['type'])) {
      throw new ServerConfigurationException(sprintf('The form element information is not valid for this field: %s.', $this->fieldName));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PublicFieldInfoBase::$categories protected property Sections for the field information.
PublicFieldInfoBase::$defaultSections protected static property Default categories for the field information.
PublicFieldInfoBase::$fieldName protected property The name of the public field.
PublicFieldInfoBase::addCategory public function Add categories to the field info. Overrides PublicFieldInfoInterface::addCategory
PublicFieldInfoBase::addSectionDefaults public function Merges default data in a section if it's not populated. Overrides PublicFieldInfoInterface::addSectionDefaults
PublicFieldInfoBase::getSection public function Gets the section. Overrides PublicFieldInfoInterface::getSection
PublicFieldInfoBase::prepare public function Generates an structured array ready to be encoded. Overrides PublicFieldInfoInterface::prepare
PublicFieldInfoBase::process protected function Processes the provided data for the section.
PublicFieldInfoBase::validate protected function Validates the provided data for the section.
PublicFieldInfoBase::validateData protected function Validates the info section.
PublicFieldInfoBase::validateFormElement protected function Validates the info section.
PublicFieldInfoBase::validateInfo protected function Validates the info section.
PublicFieldInfoBase::__construct public function PublicFieldInfoBase constructor. 1