<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Drupal-Entities on despatches</title><link>https://icle.es/tags/drupal-entities/</link><description>Recent content in Drupal-Entities on despatches</description><generator>Hugo</generator><language>en</language><lastBuildDate>Thu, 19 Jun 2025 21:38:42 +0100</lastBuildDate><atom:link href="https://icle.es/tags/drupal-entities/index.xml" rel="self" type="application/rss+xml"/><item><title>Drupal Entities - PHP Class per bundle</title><link>https://icle.es/2014/04/17/drupal-entities-php-class-per-bundle/</link><pubDate>Thu, 17 Apr 2014 11:18:08 +0000</pubDate><guid>https://icle.es/2014/04/17/drupal-entities-php-class-per-bundle/</guid><description>&lt;p>If you would like a bit more polymorphism in your drupal entities, this might
cheer you up :-D&lt;/p>
&lt;p>I was looking for a way to have a class hierarchy that matched the bundle
&amp;ldquo;hierarchy&amp;rdquo; of entities in drupal. Yes, they are all &amp;ldquo;subclasses&amp;rdquo; of ONE parent,
but it is still useful to be able to have a class per bundle.&lt;/p>
&lt;p>The
&lt;a href="https://drupal.org/project/entity_bundle_plugin" title="entity bundle plugin">entity bundle plugin&lt;/a> does
a good job of providing a plugin framework to instantiate classes per bundle
type. There is also
&lt;a href="http://bojanz.wordpress.com/2013/07/19/entity-bundle-plugin/" title="how to use entity bundle plugin">an example of how to use this&lt;/a>.
However, this was a bit of overkill for me. I did however borrow the idea (and
some code) to implement it in a simpler fashion.&lt;/p></description><content:encoded><![CDATA[<p>If you would like a bit more polymorphism in your drupal entities, this might
cheer you up :-D</p>
<p>I was looking for a way to have a class hierarchy that matched the bundle
&ldquo;hierarchy&rdquo; of entities in drupal. Yes, they are all &ldquo;subclasses&rdquo; of ONE parent,
but it is still useful to be able to have a class per bundle.</p>
<p>The
<a href="https://drupal.org/project/entity_bundle_plugin" title="entity bundle plugin">entity bundle plugin</a> does
a good job of providing a plugin framework to instantiate classes per bundle
type. There is also
<a href="http://bojanz.wordpress.com/2013/07/19/entity-bundle-plugin/" title="how to use entity bundle plugin">an example of how to use this</a>.
However, this was a bit of overkill for me. I did however borrow the idea (and
some code) to implement it in a simpler fashion.</p>
<p>Implement a custom controller and override the create and the query methods</p>
```phg
    class MyEntityAPIController extends EntityAPIController {
      /**
       * Overrides EntityAPIController::query().
       */
      public function query($ids, $conditions, $revision_id = FALSE) {
        $query = $this->buildQuery($ids, $conditions, $revision_id);
        $result = $query->execute();
        $result->setFetchMode(PDO::FETCH_ASSOC);

        // Build the resulting objects ourselves, since the standard PDO ways of
        // doing that are completely useless.
        $objects = array();
        foreach ($result as $row) {
          $row['is_new'] = FALSE;
          $objects[] = $this->create($row);
        }
        return $objects;
      }

      /**
       * Overrides EntityAPIController::create().
       */
      public function create(array $values = array()) {
        if (!isset($values[$this->entityInfo['entity keys']['bundle']])) {
          throw new Exception(t('No bundle provided to MyEntityAPIController::create().'));
        }

        $bundle = $values[$this->entityInfo['entity keys']['bundle']];
          // Add is_new property if it is not set.
        $values += array(
          'is_new' => TRUE,
        );

        $default_class = isset($this->entityInfo['entity class']) ? $this->entityInfo['entity class'] : NULL;
        $class = isset($this->entityInfo['bundles'][$bundle]['entity class']) ? $this->entityInfo['bundles'][$bundle]['entity class'] : $default_class;
        if (!class_exists($class)) {
          $class = "Entity";
        }

        return new $class($values, $this->entityType);
      }

    }
```
<p>I can now define a PHP class for each bundle as follows in hook_entity_info</p>
```php
      $entity['myentity'] = array(
        'label' => t('myentity'),
        'module' => 'mymodule',
        'entity class' => 'MyEntity',  // Default entity class if not defined in bundle
        'controller class' => 'MyEntityAPIController',
        'base table' => 'myentity',
        'entity keys' => array(
          'id' => 'id',
          'label' => 'name',
          'bundle' => 'bundle',
        ),
        'bundles' => array(
          'bundle1' => array(
            'label' => 'Bundle 1',
            'entity class' => 'Bundle1Entity',
          ),
          'bundle2' => array(
            'label' => 'Bundle 2',
            'entity class' => 'Bundle2Entity',
          ),
        ),
```
<p>Don&rsquo;t forget to clear the cache and you should be able to get bundle specific
classes instantiated. It will fall back to the &rsquo;entity class&rsquo; defined for the
entity if the bundle &rsquo;entity class&rsquo; is not defined or cannot be found.</p>
]]></content:encoded></item></channel></rss>