2

While studying the code in the Elements module, I noticed that the file elements.theme.inc is listed in elements.info, but is also listed for each theme implementation in elements_theme().

elements.info

name = Elements
description = Provides a library of Form API elements.
core = 7.x
files[] = elements.module
files[] = elements.theme.inc

elements.module -> elements_theme()

function elements_theme() {
  return array(
    'emailfield' => array(
      'arguments' => array('element' => NULL),
      'render element' => 'element',
      'file' => 'elements.theme.inc',
    ),
    'searchfield' => array(
      'arguments' => array('element' => NULL),
      'render element' => 'element',
      'file' => 'elements.theme.inc',
    ),
    'telfield' => array(
      'arguments' => array('element' => NULL),
      'render element' => 'element',
      'file' => 'elements.theme.inc',
    ),
    'urlfield' => array(
      'arguments' => array('element' => NULL),
      'render element' => 'element',
      'file' => 'elements.theme.inc',
    ),
    'numberfield' => array(
      'arguments' => array('element' => NULL),
      'render element' => 'element',
      'file' => 'elements.theme.inc',
    ),
    'rangefield' => array(
      'arguments' => array('element' => NULL),
      'render element' => 'element',
      'file' => 'elements.theme.inc',
    ),
  );
}

The Question

is there any advantage or performance improvement from listing the elements.theme.inc file in both places? Is there another reason why this might be necessary?

As always, I appreciate all responses.

2

The Elements module is using that slightly incorrectly; files[] is used to inform Drupal's autoloader of files that contain classes, so that they can be used without manually including the file they're declared in.

As elements.theme.inc doesn't contain any classes, listing it in files[] actually does nothing at all. Though it doesn't cause any problems, such is the nature of autoloading.

So to directly answer the question, there can't really be an advantage of using one method over another, as they're used for different things. If elements.theme.inc contained a class as well as the theme functions, it would be perfectly valid to have both declarations.

As an aside, I've seen this plenty of times before - there was a bit of confusion when D7 first came out as to what files[] was for, and quite a few people chose to list all of, or the 'main' files of, their module.

2
  • Thanks, Clive! Somehow, I knew you'd be the one who would clear this up for me. Cheers! Mar 30 '16 at 17:06
  • Happy to help :)
    – Clive
    Mar 30 '16 at 17:10
1

Theme files should be included in hook_theme().

https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_theme/7

file: The file the implementation resides in. This file will be included prior to the theme being rendered, to make sure that the function or preprocess function (as needed) is actually loaded; this makes it possible to split theme functions out into separate files quite easily.

Files, declared in .info should include dynamic-loading code registry (class or interface). https://www.drupal.org/node/542202#files

Drupal supports a dynamic-loading code registry. To support it, all modules must declare any code files containing class or interface declarations in the .info file. When a module is enabled, Drupal will rescan all declared files and index all the classes and interfaces that it finds. Classes will be loaded automatically by PHP when they are first accessed.

1
  • Good point, I forgot about interfaces. I wonder if they've updated it for traits now that php has them
    – Clive
    Mar 30 '16 at 17:10

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.