{"id":20782412,"url":"https://github.com/vishwac09/entity-convert","last_synced_at":"2026-05-15T12:34:03.324Z","repository":{"id":59007889,"uuid":"528358052","full_name":"vishwac09/entity-convert","owner":"vishwac09","description":"Converts Drupal entity objects (Node/User/Taxonomy/File) into simple array of fields or object.","archived":false,"fork":false,"pushed_at":"2022-09-22T16:47:49.000Z","size":106,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-17T16:53:39.608Z","etag":null,"topics":["drupal","drupal-8","drupal-9","php","php-library"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vishwac09.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-08-24T09:42:52.000Z","updated_at":"2023-04-09T04:13:00.000Z","dependencies_parsed_at":"2022-09-11T04:31:09.174Z","dependency_job_id":null,"html_url":"https://github.com/vishwac09/entity-convert","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/vishwac09/entity-convert","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vishwac09%2Fentity-convert","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vishwac09%2Fentity-convert/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vishwac09%2Fentity-convert/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vishwac09%2Fentity-convert/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vishwac09","download_url":"https://codeload.github.com/vishwac09/entity-convert/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vishwac09%2Fentity-convert/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33067359,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-15T11:35:32.926Z","status":"ssl_error","status_checked_at":"2026-05-15T11:35:31.362Z","response_time":103,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["drupal","drupal-8","drupal-9","php","php-library"],"created_at":"2024-11-17T14:10:55.907Z","updated_at":"2026-05-15T12:34:03.310Z","avatar_url":"https://github.com/vishwac09.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EntityConvert\n\n[![Test Cases](https://github.com/vishwac09/entity-convert/actions/workflows/simple-entities-run-tests.yml/badge.svg?branch=master)](https://github.com/vishwac09/entity-convert/actions/workflows/simple-entities-run-tests.yml)\n\n[![Latest Stable Version](http://poser.pugx.org/drupal-utils/entity-convert/v)](https://packagist.org/packages/drupal-utils/entity-convert) [![Total Downloads](http://poser.pugx.org/drupal-utils/entity-convert/downloads)](https://packagist.org/packages/drupal-utils/entity-convert) [![Latest Unstable Version](http://poser.pugx.org/drupal-utils/entity-convert/v/unstable)](https://packagist.org/packages/drupal-utils/entity-convert) [![License](http://poser.pugx.org/drupal-utils/entity-convert/license)](https://packagist.org/packages/drupal-utils/entity-convert)\n\nPHP Library built to be used with **Drupal** codebase/modules only. **Parses** and simplifies the passed Node/Taxonomy/User/File (drupal entities) fully loaded object into a Simple array of fields or an Object which can be used directly to build any view or REST response layer.\n\nUsually we implement helper classes/methods to resolve the field values for a given entity instance, the idea behind this is always to reduce duplicate code and to provide a uniform interface to access the field values.\n\nEvery Drupal Entity instance also has a ```toArray()``` method associated with it, which returns all the field values as an array, which is also a good way to access all the values but the only limitation i found with using the toArray() method is all the values are returned as string hence loosing it original type and the way they are rendered. This library tries to return value as a single level and also accepts a parameter while parsing the Entity object so to maintain its original data types (useful when building REST API's).\n\n```php\nuse Drupal\\node\\Entity\\Node;\n\n$node = Node::load(1);\n$node-\u003etoArray();\n```\n\n\u003e I decided to write this library to reduce the need to write additional helper classes/methods every time we introduce a new Entity type.\n\n---\n\n### Install\n\n\u003cins\u003e**Get from Packagist using Composer**\u003c/ins\u003e\n\n1. From the root of your Drupal project run. Link to [Packagist](https://packagist.org/packages/drupal-utils/entity-convert)\n```bash\ncomposer require drupal-utils/entity-convert:0.0.1-alpha\n```\n\n\n### Usage\n\nConsider Entity type \"**Node**\" having associated the below default field types to it\n- Boolean\n- Integer, Decimal\n- List Integer / Decimal / Text\n- File / Images / Taxonomy / Content\n- Timestamp / Date\n- Link, Email\n\n\u003cins\u003e**Drupal way**\u003c/ins\u003e\n\nHow field value is accessed normally in the code.\n\n```php\n\n$node = Node::load(1);\n$field_boolean_value = $node-\u003eget('field_boolean_value')-\u003evalue;\n\n// Accessing the values of BaseFields is easy.\n\n$nid = $node-\u003eid();\n$title = $node-\u003egetTitle();\n$type = $node-\u003egetType();\n$isPromoted = $node-\u003eisPromoted();\n```\n\n**Accessing the value of fields associated with a Node entity**\n\n\u003cins\u003eBoolean Field\u003c/ins\u003e\n\n```php\n$field_boolean_value = $node-\u003eget('field_bool_multi_value')-\u003evalue;\nvar_dump($field_boolean_value);\nstring(1) \"1\"\n```\n\n\u003cins\u003eDateTime Field\u003c/ins\u003e\n\n```php\n$field_datetime_multi_value = $node-\u003eget('field_datetime_multi_value')-\u003evalue;\nvar_dump($field_datetime_multi_value);\nstring(19) \"2022-08-31T00:31:28\"\n```\n\n\u003cins\u003eDate Field\u003c/ins\u003e\n\n```php\n$field_date_multi_value = $node-\u003eget('field_date_multi_value')-\u003evalue;\nvar_dump($field_date_multi_value);\narray(2) { [0]=\u003e array(1) { [\"value\"]=\u003e string(10) \"2022-08-14\" } [1]=\u003e array(1) { [\"value\"]=\u003e string(10) \"2022-08-31\" } }\n```\n\n\u003cins\u003eEmail Field\u003c/ins\u003e\n\n```php\n$field_email_multi_value = $node-\u003eget('field_email_multi_value')-\u003evalue;\nvar_dump($field_email_multi_value);\narray(2) { [0]=\u003e array(1) { [\"value\"]=\u003e string(24) \"test@abc.com\" } [1]=\u003e array(1) { [\"value\"]=\u003e string(25) \"test@def.com\" } }\n```\n\n\u003cins\u003eFile Field\u003c/ins\u003e\n\n```php\n$field_file_multi_value = $node-\u003eget('field_file_multi_value')-\u003evalue;\nvar_dump($field_file_multi_value);\narray(1) { [0]=\u003e array(3) { [\"target_id\"]=\u003e string(1) \"3\" [\"display\"]=\u003e string(1) \"1\" [\"description\"]=\u003e string(8) \"CSV File\" } }\n```\n\n\u003cins\u003eList Text Field\u003c/ins\u003e\n\n```php\n$field_listtext_multi_value = $node-\u003eget('field_listtext_multi_value')-\u003egetValue();\nvar_dump($field_listtext_multi_value);\narray(2) { [0]=\u003e array(1) { [\"value\"]=\u003e string(5) \"apple\" } [1]=\u003e array(1) { [\"value\"]=\u003e string(4) \"ball\" } } \n```\n\n\u003cins\u003e**Using the library**\u003c/ins\u003e\n\n```php\nuse Drupal\\node\\Entity\\Node;\n// Include the library.\nuse DrupalUtils\\EntityConvert\\EntityConvert;\n \n// Load instance of node\n$node = Node::load(1);\n \n// Creating a new Instance.\n$entityConvert = new EntityConvert();\n \n// Pass on the Node object to get the parsed value as array.\n$parsedNode = $entityConvert-\u003etoArray($node, false);\n```\n\n\u003cins\u003e**Get response as array of field =\u003e values**\u003c/ins\u003e\n\n```php\n// Pass on the Node object to get the parsed value as array.\n$parsedNode = $entityConvert-\u003etoArray($node, false);\n\nvar_dump($parsedNode);\n\n^ array:42 [▼\n  \"nid\" =\u003e 1\n  \"uuid\" =\u003e \"8270a03c-a95f-4033-84a7-361597b581a9\"\n  \"vid\" =\u003e 39\n  \"langcode\" =\u003e \"en\"\n  \"type\" =\u003e 0\n  \"revision_timestamp\" =\u003e 1660994564\n  \"revision_uid\" =\u003e 1\n  \"revision_log\" =\u003e null\n  \"status\" =\u003e true\n  \"uid\" =\u003e 1\n  \"title\" =\u003e \"Car 1\"\n  \"created\" =\u003e 1659367826\n  \"changed\" =\u003e 1660994564\n  \"promote\" =\u003e true\n  \"sticky\" =\u003e true\n  \"default_langcode\" =\u003e true\n  \"revision_default\" =\u003e true\n  \"revision_translation_affected\" =\u003e true\n  \"path\" =\u003e array:3 [▶]\n  \"field_bool_multi_value\" =\u003e array:1 [▼\n    0 =\u003e true\n  ]\n  \"field_comments_single_value\" =\u003e array:1 [▶]\n  \"field_content_multi_value\" =\u003e array:1 [▶]\n  \"field_datetime_multi_value\" =\u003e array:1 [▼\n    0 =\u003e \"2022-08-31T00:31:28\"\n  ]\n  \"field_date_multi_value\" =\u003e array:2 [▼\n    0 =\u003e \"2022-08-14\"\n    1 =\u003e \"2022-08-31\"\n  ]\n  \"field_email_multi_value\" =\u003e array:2 [▼\n    0 =\u003e \"testing@abc.com\"\n    1 =\u003e \"testing@def.com\"\n  ]\n  \"field_file_multi_value\" =\u003e array:1 [▼\n    0 =\u003e array:11 [▼\n      \"fid\" =\u003e \"3\"\n      \"uuid\" =\u003e \"6eb73b6e-be50-449e-a57d-bccf9d6b0bc0\"\n      \"langcode\" =\u003e \"en\"\n      \"uid\" =\u003e \"1\"\n      \"filename\" =\u003e \"testing.csv\"\n      \"uri\" =\u003e \"public://2022-08/testing.csv\"\n      \"filemime\" =\u003e \"text/csv\"\n      \"filesize\" =\u003e \"96\"\n      \"status\" =\u003e \"1\"\n      \"created\" =\u003e \"1660992978\"\n      \"changed\" =\u003e \"1660993028\"\n    ]\n  ]\n  \"field_image_multi_value\" =\u003e array:1 [▶]\n  \"field_link_multi_value\" =\u003e array:1 [▼\n    0 =\u003e array:3 [▶]\n  ]\n  \"field_listint_multi_value\" =\u003e array:1 [▶]\n  \"field_listtext_multi_value\" =\u003e array:2 [▶]\n  \"field_list_multi_value\" =\u003e array:1 [▶]\n  \"field_numberfloat_multi_value\" =\u003e array:1 [▶]\n  \"field_numberint_multi_value\" =\u003e array:1 [▶]\n  \"field_textflws_multi_value\" =\u003e array:1 [▶]\n]\n```\n\n#### Get response as an object\n\nIf the response is an object, all available fields/values can be accessed as a property.\n\n```php\n// Pass on the Node object to get the parsed value as array.\n$parsedNode = $entityConvert-\u003etoObject($node, true);\n\nvar_dump($parsedNode-\u003enid);\nint(1)\n\nvar_dump($parsedNode-\u003etitle);\nstring(5) \"Car 1\"\n\nvar_dump($parsedNode-\u003efield_file_multi_value);\narray(1) { [0]=\u003e array(11) { [\"fid\"]=\u003e string(1) \"3\" [\"uuid\"]=\u003e string(36) \"6eb73b6e-be50-449e-a57d-bccf9d6b0bc0\" [\"langcode\"]=\u003e string(2) \"en\" [\"uid\"]=\u003e string(1) \"1\" [\"filename\"]=\u003e string(40) \"testing.csv\" [\"uri\"]=\u003e string(57) \"public://2022-08/testing.csv\" [\"filemime\"]=\u003e string(8) \"text/csv\" [\"filesize\"]=\u003e string(2) \"96\" [\"status\"]=\u003e string(1) \"1\" [\"created\"]=\u003e string(10) \"1660992978\" [\"changed\"]=\u003e string(10) \"1660993028\" } } \n```\n\n### API\n\nThe methods toArray/toObject (instance, strict_type) accepts 2 arguments.\n    \n    - instance = Loaded instance object of the type Node/User/Taxonomy/File.\n    - strict_type = Boolean: Returned response has all data types preserved.\n\nWhen we get value from field attached to an Entity, drupal will usually return all values as string. Sending second parameter as true, the library will typecast all the value to correct data type.\n\n**Reference**\n\n| Method  | Parameters | Comment |\n| ------------- | ------------- | ------------- |\n| toArray  | ```$instance```  | Object of type Node/User/Taxonomy or File |\n| \u0026nbsp; | ```$strict_type``` = false | Boolean flag representing whether value to be type casted|\n| toObject  | ```$instance```  | Object of type Node/User/Taxonomy or File |\n| \u0026nbsp; | ```$strict_type``` = false | Boolean flag representing whether value to be type casted|\n\n```php\nuse DrupalUtils\\EntityConvert\\EntityConvert;\n\n$entityConvert = new EntityConvert();\n// $instance == Node/User/Taxonomy/File.\n$entityConvert-\u003etoArray($instance, true);\n$entityConvert-\u003etoObject($instance, false);\n\n// The toArray method accepts 2 arguments.\n\n/**\n * Parse the given entity instance and return array.\n *\n * @param Object $instance\n *   The Entity instance to parse.\n * @param Boolean $strict_type\n *   Flag indicating variable types should be preserved.\n *\n * @return array\n */\nfunction toArray($instance, $strict_type = false);\n\n// The toObject method accepts 2 arguments.\n\n/**\n * Parse the given entity instance and return object.\n *\n * @param Object $instance\n *   The Entity instance to parse.\n * @param Boolean $strict_type\n *   Flag indicating variable types should be preserved.\n *\n * @return EntityInterface\n */\nfunction toObject($instance = null, $strict_type = false)\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvishwac09%2Fentity-convert","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvishwac09%2Fentity-convert","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvishwac09%2Fentity-convert/lists"}