Quantcast
Viewing all articles
Browse latest Browse all 293221

DX: Helper method/function to retrieve reference fields related to a module

Problem/Motivation

In Drupal 7, if you write a module that uses field storage to write on entity_save() -- say, Taxonomy Access Control -- it is difficult to know what fields actually contain the data that you care about. In Drupal 8, we have Entity Reference in core, which compounds the issue because it should be used as the default entity-to-entity reference system.

Certainly, a module could provide its own field with a specific field name, but that approach defeats the purpose of having flexible field configuration and storage. What we need instead is a reusabe way for developers to know which fields they should be watching when an entity is saved, updated or deleted.

Proposed resolution

Add a function or method to the Field API that retrieves data related to a specific entity or module.

Pseudo-code:

<?php
function foo_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
 
$module = 'foo';
 
$my_properties = $entity->getField($module);
 
// Returns an array of property names.
 
foreeach ($my_properties as $key) {
   
$data = $entity->{$key};
   
foo_save($data, $entity->id);
  }
}
?>

Think about this in the context of access control, in particular.

Remaining tasks

Code to be written.

User interface changes

None.

API changes

New utility function or method for Field API.


Viewing all articles
Browse latest Browse all 293221

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>