<?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>Php on despatches</title><link>https://icle.es/tags/php/</link><description>Recent content in Php on despatches</description><generator>Hugo</generator><language>en</language><lastBuildDate>Fri, 20 Jun 2025 08:42:17 +0100</lastBuildDate><atom:link href="https://icle.es/tags/php/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><item><title>hook_theme doesn't get called</title><link>https://icle.es/2011/02/22/hook_theme-doesnt-get-called/</link><pubDate>Tue, 22 Feb 2011 20:20:11 +0000</pubDate><guid>https://icle.es/2011/02/22/hook_theme-doesnt-get-called/</guid><description>&lt;p>I was developing a new module in drupal and it needed a theme function to be
implemented.&lt;/p>
&lt;p>As per the instructions, it was implemented as follows (to use a template)&lt;/p>
```phg
/**
 * Implementation of hook_theme().
 */
function my_module_results_theme($existing, $type, $theme, $path) {

 return array(
 'my_block' => array(
 'template' => 'my_block',
 'arguments' => array(
 'var1' => NULL
 )
 )
 );
}
```
&lt;p>However, when trying to apply the theme, it didn&amp;rsquo;t work. I tried various things
and identified that the hook above was just not being called. A little bit of
digging helped me discover that themes are cached. This happens even in the dev
mode. To resolve this, go to&lt;/p></description><content:encoded><![CDATA[<p>I was developing a new module in drupal and it needed a theme function to be
implemented.</p>
<p>As per the instructions, it was implemented as follows (to use a template)</p>
```phg
/**
 * Implementation of hook_theme().
 */
function my_module_results_theme($existing, $type, $theme, $path) {

    return array(
        'my_block' => array(
            'template' => 'my_block',
            'arguments' => array(
                'var1' => NULL
            )
        )
    );
}
```
<p>However, when trying to apply the theme, it didn&rsquo;t work. I tried various things
and identified that the hook above was just not being called. A little bit of
digging helped me discover that themes are cached. This happens even in the dev
mode. To resolve this, go to</p>
<p><code>Administer  -&gt; Performance -&gt; Clear Cached Data</code> (right at the bottom of the
page)</p>
<p>and et voila my theme was now being utilised.</p>
]]></content:encoded></item></channel></rss>