<?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 on despatches</title><link>https://icle.es/tags/drupal/</link><description>Recent content in Drupal 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/drupal/index.xml" rel="self" type="application/rss+xml"/><item><title>Drupal 8 error: "The following reasons prevent the modules from being uninstalled: Fields pending deletion"</title><link>https://icle.es/2017/09/28/drupal-8-error-the-following-reasons-prevent-the-modules-from-being-uninstalled-fields-pending-deletion/</link><pubDate>Thu, 28 Sep 2017 15:53:15 +0000</pubDate><guid>https://icle.es/2017/09/28/drupal-8-error-the-following-reasons-prevent-the-modules-from-being-uninstalled-fields-pending-deletion/</guid><description>&lt;p>When you try and uninstall a module which has a field that you have used, it can
throw the following error:&lt;/p>
```
The following reasons prevent the modules from being uninstalled: Fields pending
deletion\
```
&lt;p>This is an issue in both Drupal 7 and Drupal 8. This is due to the fact that
drupal doesn't actually delete the data for the field when you delete the
field. It deletes the data during cron runs. If cron hasn't been run enough
times since you deleted the field, drupal won't let you uninstall the module.&lt;/p></description><content:encoded><![CDATA[<p>When you try and uninstall a module which has a field that you have used, it can
throw the following error:</p>
```
The following reasons prevent the modules from being uninstalled: Fields pending
deletion\
```
<p>This is an issue in both Drupal 7 and Drupal 8. This is due to the fact that
drupal doesn't actually delete the data for the field when you delete the
field. It deletes the data during cron runs. If cron hasn't been run enough
times since you deleted the field, drupal won't let you uninstall the module.</p>
<p>To force drupal to purge the data, you can run the following command</p>
```bash
drush php-eval \'field_purge_batch(500);\'\
```
<p>Increase 500 to a high enough number to wipe out the data. Afte this has
completed, you should be able to uninstall the module</p>
<p>References:<br>
<a href="https://drupal.stackexchange.com/questions/184690/module-uninstall-dependencies">Module uninstall dependencies (drupal stackexchange)</a><br>
<a href="https://www.drupal.org/node/1331922">Message &quot;Required by Drupal (Fields Pending Deletion)&quot; baffles users</a><br>
<a href="https://www.drupal.org/node/2835035">Can't uninstall YAML because of following reason: Fields pending deletion</a></p>
]]></content:encoded></item><item><title>Drupal 7 + Services: Paging &amp;amp; Filtering the index endpoint</title><link>https://icle.es/2017/09/03/drupal-7-services-paging-filtering-the-index-endpoint/</link><pubDate>Sun, 03 Sep 2017 12:20:54 +0000</pubDate><guid>https://icle.es/2017/09/03/drupal-7-services-paging-filtering-the-index-endpoint/</guid><description>&lt;p>There are a lot of ways to manipulate the data returned by the index endpoint.
In this post, we are going to consider the node index endpoint. By default, this
endpoint returns all nodes sorted in descending order of last update with 20
items per page.&lt;/p>
&lt;p>You access the node index endpoint by going to&lt;/p>
&lt;p>&lt;code>http://&amp;lt;domain&amp;gt;/&amp;lt;endpoint-pathnode.json&lt;/code> (or the alias given to node in the
resources section)&lt;/p>
&lt;p>You can replace .json with with other extensions to get the same data in
different formats&lt;/p></description><content:encoded><![CDATA[<p>There are a lot of ways to manipulate the data returned by the index endpoint.
In this post, we are going to consider the node index endpoint. By default, this
endpoint returns all nodes sorted in descending order of last update with 20
items per page.</p>
<p>You access the node index endpoint by going to</p>
<p><code>http://&lt;domain&gt;/&lt;endpoint-pathnode.json</code> (or the alias given to node in the
resources section)</p>
<p>You can replace .json with with other extensions to get the same data in
different formats</p>
<p>To access the second page, you can use the page parameter</p>
```
node.json?page=2
node.json?page=5
```
<p>To change the number of items on each page, you need the &ldquo;perform unlimited
index&rdquo; queries permission. You use the <code>pagesize</code> parameter to change it</p>
```
node.json?pagesize=100
node.json?pagesize=50
```
<p>To filter a field, you can use the parameters[property] where &lsquo;property&rsquo; is the
field on which you want to filter. It needs to be a field on the node table, and
not a drupal field as it does not do the joins to pull in field data.</p>
```
node.json?parameters[type]=blog_post
node.json?parameters[type]=article
```
<p>To apply a different filter than of equality, you can use
options[parameters_op][property] where property is the same as above.</p>
```
node.json?parameters[created]=1431065220&options[parameters_op][created]=<
node.json?parameters[changed]=1431065220&options[parameters_op][changed]=>
```
<p>To return fewer fields, you can use fields and comma separate the properties.
Once again, you can only specify properties on the entity (i.e fields on the
base table)</p>
```
node.json?fields=nid,changed
node.json?fields=nid,created,title
```
<p>you can sort the results by using options<code>[property]=&lt;asc|desc&gt;</code></p>
```
node.json?options[orderby][nid]=asc
node.json?options[orderby][created]=desc
```
<p>You can also mix and match these separate options</p>
```
node.json?page=10&pagesize=100&parameters[type]=blog_post&options[parameters_op][type]=!=&fields=nid,changed
```
]]></content:encoded></item><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>