Created
March 3, 2014 22:10
-
-
Save Berdir/9335736 to your computer and use it in GitHub Desktop.
Drupal Content Entity Parser for Kint, place in parsers/custom
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Drupal\Core\Entity\ContentEntityInterface; | |
class Kint_Parsers_ContentEntity extends kintParser | |
{ | |
private static $cache = array(); | |
protected function _parse(&$variable) | |
{ | |
if ((!$variable instanceof ContentEntityInterface)) { | |
return false; | |
} | |
$entity_type_id = $variable->getEntityTypeId(); | |
$bundle = $variable->bundle(); | |
// Assuming field definitions will not change inside one request. | |
if(!isset(self::$cache[$entity_type_id][$bundle])) { | |
self::$cache[$entity_type_id][$bundle] = array(); | |
// Loop over fields. | |
foreach($variable->getFieldDefinitions() as $field_name => $field_definition) { | |
$output = new \kintVariableData(); | |
$output->name = $field_definition->getLabel() . ' (' . $field_name . ')'; | |
$output->type = 'field'; | |
$output->subtype = $field_definition->getType(); | |
$output->operator = '->'; | |
$output-> | |
// @todo: Support multi-value fields. | |
$item = $variable->get($field_name)->first(); | |
// Loop over properties of that field. | |
foreach ($field_definition->getPropertyDefinitions() as $property => $definition) { | |
$value = $item->get($property)->getValue(); | |
$property_value = static::factory($value, $property); | |
$property_value->operator = '->'; | |
$output->extendedValue[] = $property_value; | |
} | |
$output->size = count($field_definition->getPropertyDefinitions()); | |
self::$cache[$entity_type_id][$bundle][$field_name] = $output; | |
} | |
} | |
$this->value = self::$cache[$entity_type_id][$bundle]; | |
$this->type = 'Fields'; | |
$this->size = count(self::$cache[$entity_type_id][$bundle]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment