Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
80.00% |
4 / 5 |
CRAP | |
93.55% |
29 / 31 |
Data | |
0.00% |
0 / 1 |
|
80.00% |
4 / 5 |
18.09 | |
93.55% |
29 / 31 |
get | |
0.00% |
0 / 1 |
13.11 | |
91.30% |
21 / 23 |
|||
getBundleType | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
getEntityType | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
setEntityType | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
isField | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
<?php | |
namespace Drupal\data_api; | |
class Data extends \AKlump\Data\Data | |
{ | |
protected $entityType = null; | |
/** | |
* @inheritdoc | |
*/ | |
public function get($subject, $path, $defaultValue = null, $valueCallback = null) | |
{ | |
if (empty($subject)) { | |
return $defaultValue; | |
} | |
// This will make sure $path is an array. | |
$this->validate($subject, $path, $defaultValue); | |
// When we know the entity type, we can use Drupal's field api functions. | |
// This will take care of the translation. | |
$processed = false; | |
if (count($path) <= 3 && ($bundle = $this->getBundleType($subject))) { | |
if (($field_name = reset($path)) && $this->isField($bundle, $field_name)) { | |
array_shift($path); | |
$processed = true; | |
$items = field_get_items($this->getEntityType(), $subject, $field_name); | |
if (count($path) === 0) { | |
$value = !empty($items) ? $items : $defaultValue; | |
return $this->postGet($value, $defaultValue, $valueCallback); | |
} | |
$delta = array_shift($path); | |
if (count($path) === 0) { | |
$value = array_key_exists($delta, $items) ? $items[$delta] : $defaultValue; | |
return $this->postGet($value, $defaultValue, $valueCallback); | |
} | |
$key = array_shift($path); | |
if (count($path) === 0) { | |
$value = array_key_exists($key, $items[$delta]) ? $items[$delta][$key] : $defaultValue; | |
return $this->postGet($value, $defaultValue, $valueCallback); | |
} | |
} | |
} | |
// If we've processed per Drupal API functions then return, otherwise lean on parent class. | |
return $processed ? $subject : parent::get($subject, $path, $defaultValue, $valueCallback); | |
} | |
/** | |
* Return the bundle type of the entity. | |
* | |
* @param string $subject | |
* | |
* @return null|bool | |
*/ | |
protected function getBundleType($subject) | |
{ | |
if (empty($entity_type = $this->getEntityType())) { | |
return null; | |
} | |
list(, , $bundle) = entity_extract_ids($entity_type, $subject); | |
return $bundle; | |
} | |
/** | |
* @return string|null | |
*/ | |
protected function getEntityType() | |
{ | |
return $this->entityType; | |
} | |
/** | |
* @param string $entity_type | |
* | |
* @return $this | |
*/ | |
public function setEntityType($entity_type) | |
{ | |
$this->entityType = $entity_type; | |
return $this; | |
} | |
/** | |
* Tests if $field is a field instance on $bundle. | |
* | |
* @param string $bundle | |
* @param string $field | |
* | |
* @return bool | |
*/ | |
protected function isField($bundle, $field) | |
{ | |
return array_key_exists($field, field_info_instances($this->getEntityType(), $bundle)); | |
} | |
} |