View source
<?php
namespace Drupal\restful\Plugin\resource\Field\PublicFieldInfo;
use Drupal\restful\Exception\ServerConfigurationException;
class PublicFieldInfoBase implements PublicFieldInfoInterface {
protected static $defaultSections = array(
'info' => array(
'label' => '',
'description' => '',
),
'data' => array(
'type' => NULL,
'read_only' => FALSE,
'cardinality' => 1,
'required' => FALSE,
),
'form_element' => array(
'type' => NULL,
'default_value' => '',
'placeholder' => '',
'size' => NULL,
'allowed_values' => NULL,
),
);
protected $categories = array();
protected $fieldName = '';
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);
}
}
public function prepare() {
return $this->categories;
}
public function addCategory($category_name, array $section_info) {
try {
$this
->validate($category_name, $section_info);
$this->categories[$category_name] = $this
->process($category_name, $section_info);
} catch (ServerConfigurationException $e) {
}
}
public function getSection($section_name) {
return empty($this->categories[$section_name]) ? array() : $this->categories[$section_name];
}
public function addSectionDefaults($section_name, array $section_info) {
$this
->addCategory($section_name, array_merge($section_info, $this
->getSection($section_name)));
}
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);
}
}
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') {
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;
}
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));
}
}
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));
}
}
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));
}
}
}