1

I'm attempting to understand the concept of entity storage. For example in this tutorial

https://www.sitepoint.com/drupal-8-version-entityfieldquery/

it's using code to load the nodes from nids in a similar way to this

$ids = \Drupal::entityQuery('node')
    ->condition('status', 1)
    ->execute();

$node_storage = \Drupal::entityManager()->getStorage('node');

// Load multiple nodes
$node_storage->loadMultiple($ids);

But I'm not sure exactly what this is doing

$node_storage = \Drupal::entityManager()->getStorage('node');

My best guess is that it's loading the format of how the node is stored in the database so it knows how to load everything based on that format, but that's just a guess.

Also side note: the path I normally take is doing a foreach loop on the query results and loading the node based on the nid inside that. Like so. But I'm not sure what's best practice.

        foreach ($ids as $nid) {
            $node  = Node::load($nid);
        }

What exactly is entityStorage, and what function is it providing?

  • 2
    It's literally an interface to the storage backend for the entity data. That could be MySQL, SQLite, mongo, hand-written and managed by a guy interpreting the blinking HDD lights as morse code, whatever. It exists so Drupal (and you) can call predictable methods to get data, but not worry about where or how the underlying data is stored. Loading entities in a loop like that is inefficient when you consider there's a loadMultiple method available – Clive Aug 15 '17 at 15:44
  • 2
    Couldn't find an example for the morse code, but this is also a nice one External Entities drupal.org/project/external_entities and @Matt Node::load() is only a shortcut to the entity storage, see api.drupal.org/api/drupal/… – 4k4 Aug 15 '17 at 16:00
  • This is really cool, and I suppose I can see applications where it might come in handy, especially in all situations where morse code is the only option. But if I created my own entity storage interface how would I tell drupal to switch to using my own instead of the default one for nodes. For instance if I clicked add content and submitted a basic page. How do I tell it to save it in morse code instead. I'd want to switch to the new format site wide, not just for reading and saving things in a plugin. – Matt Aug 15 '17 at 17:50
  • 1
    Also worth pointing out that Node::load() is simply a helper method that gets the entity storage handler to then call load on that. There aren't two different API's, it's just a shortcut. And there is a loadMultiple() too, you should never call load() in a loop – Berdir Aug 15 '17 at 19:00
  • 1
    @Matt Only if the module was being "naughty" and not using the interface. All user space code should be using the interface, precisely so the underlying storage can change without breaking anything. Program to the interface, not the implementation, as they say – Clive Aug 15 '17 at 19:06
4

Entity storage is a storage controller for the entity that handles the actual CRUD operations of entity data in the storage. That means it is responsible for inserting and deleting the entity data in the actual storage/repository.

The storage can be a .yaml file (for config entities) or a SQL database (for content entities). There are some specific use cases for null storage (like e-mail message) which basically means no data is stored anywhere and entity with such storage is just virtual: It exists only per single request as you define it.

You can additionally write your own storage controller and use remote storage, a file, a stream or whatever the requirements are.

  • 1
    storage handler, we don't use the term controller anymore in the entity system to avoid confusion with routing and other uses of controller. – Berdir Aug 15 '17 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.