View source
<?php
namespace Drupal\restful\Plugin\resource\Field\PublicFieldInfo;
class PublicFieldInfoEntity extends PublicFieldInfoBase implements PublicFieldInfoEntityInterface {
protected $property;
protected $entityType;
protected $bundle;
public function __construct($field_name, $property, $entity_type, $bundle, array $sections = array()) {
parent::__construct($field_name, $sections);
$this->property = $property;
$this->entityType = $entity_type;
$this->bundle = $bundle;
}
public function getFormSchemaAllowedValues() {
if (!module_exists('options')) {
return NULL;
}
$field_name = $this->property;
if (!($field_info = field_info_field($field_name))) {
return NULL;
}
if (!($field_instance = field_info_instance($this->entityType, $field_name, $this->bundle))) {
return NULL;
}
if (!$this::formSchemaHasAllowedValues($field_info, $field_instance)) {
return NULL;
}
$type = str_replace('options_', '', $field_instance['widget']['type']);
$multiple = $field_info['cardinality'] > 1 || $field_info['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
$required = TRUE;
$has_value = TRUE;
$properties = _options_properties($type, $multiple, $required, $has_value);
$values = array();
$entity_info = $this
->getEntityInfo();
if (!empty($entity_info['entity keys']['bundle'])) {
$values[$entity_info['entity keys']['bundle']] = $this->bundle;
}
$entity = entity_create($this->entityType, $values);
return _options_get_options($field_info, $field_instance, $properties, $this->entityType, $entity);
}
public function getFormSchemaAllowedType() {
if (!module_exists('options')) {
return NULL;
}
$field_name = $this->property;
if (!($field_info = field_info_field($field_name))) {
return NULL;
}
if (!($field_instance = field_info_instance($this->entityType, $field_name, $this->bundle))) {
return NULL;
}
return $field_instance['widget']['type'];
}
protected function getEntityInfo($type = NULL) {
return entity_get_info($type ? $type : $this->entityType);
}
protected static function formSchemaHasAllowedValues($field, $field_instance) {
$field_types = array(
'entityreference',
'taxonomy_term_reference',
'field_collection',
'commerce_product_reference',
);
$widget_types = array(
'taxonomy_autocomplete',
'entityreference_autocomplete',
'entityreference_autocomplete_tags',
'commerce_product_reference_autocomplete',
);
return !in_array($field['type'], $field_types) || !in_array($field_instance['widget']['type'], $widget_types);
}
}