This project is not covered by Drupal’s security advisory policy.
Entity Parser is a utility module which provides a possibility to manage Entity Object (Node,Taxonomy term , User and Block Content) to simple array or another custom type such as JSON format . The concept of this module is similar as view mode but adopt in the fields ouput format
The list of Entity Parser Functionnality
Entity Parser with twig extension
Entity Parser custom entity fields output
Simple example of how to implement in your drupal project
- Create a Class for example EntityParserDemo.php extend Drupal\entity_parser\EntityParser
namespace Drupal\entity_parser_demo;
use Drupal\entity_parser\EntityParser;
class EntityParserDemo extend EntityParser {
//**** field hook ALIAS = blockA ***//
function blockA_changed($entity, $field) {
$item =$this->string($entity, $field);
$changed_date = date("F j, Y, g:i a", array_shift($item));
return $changed_date ;
}
function blockA_uid($entity, $field) {
$uid = array_shift ($this->integer($entity, $field));
$item_user = \Drupal\user\Entity\User::load($uid);
$result=[];
if (is_object($item_user)) {
$result = [
"user_object" => $item_user,
"name" => $item_user->getUsername(),
"email" => $item_user->getEmail()
];
}
return $result ;
}
//**** field hook ALIAS = blockB ***//
function blockB_changed($entity, $field) {
return $this->string($entity, $field);
}
}
How to use
$parser = new EntityParserDemo();
$nid = 1;
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
// getting default fields in array format
$node_array_full = $parser->node_parser($node);
// get field hook ALIAS = blockA
$options =array( '#hook_alias' => 'blockA');
$fields = ['changed','uid'] ;
$node_blookA = $parser->node_parser($node,$fields,$options));
// get field hook ALIAS = blockB
$options =array( '#hook_alias' => 'blockB');
$fields = ['changed'] ;
$node_blookB = $parser->node_parser($node,$fields,$options));
print "1- changed date for ALIAS blockA : " . $node_blookA['changed'];
print "2- user name :" . reset($node_blookA['uid'])['name'];
print "3- changed date for ALIAS blockB : " . $node_blookB['changed'];
//output result
// 1- changed date for ALIAS blockA : March 10, 2001, 5:16 pm
// 2- user name : miandry
// 3- changed date for ALIAS blockB : 20010310
Service is avalaible entity_parser 8.x-3.1
$node = \Drupal::entityTypeManager()->getStorage("node")->load(39);
$node_array = \Drupal::service('entity_parser.manager')->parser($node);
with #hook_class
$node = \Drupal::entityTypeManager()->getStorage("node")->load(39);
$options['#hook_class'] ='\Drupal\extend_entity_parser\ExtendEntityParser';
$options['#hook_alias'] ='mzalias';
$node_array = \Drupal::service('entity_parser.manager')->parser($node,[],$options);
kint($node_array);
Extend Class entity_parser

Output

Project information
Seeking new maintainer
The current maintainers are looking for new people to take ownership.- Project categories: Developer tools
20 sites report using this module
- Created by miandry on , updated
This project is not covered by the security advisory policy.
Use at your own risk! It may have publicly disclosed vulnerabilities.


