You are here

function ctools_plugin_get_class in Chaos Tool Suite (ctools) 6

Same name and namespace in other branches
  1. 7 includes/plugins.inc \ctools_plugin_get_class()

Get a class from a plugin, if it exists. If the plugin is not already loaded, try ctools_plugin_load_class() instead.

Parameters

$plugin_definition: The loaded plugin type.

$class_name: The identifier of the class. For example, 'handler'.

$abstract: If true, will return abstract classes. Otherwise, parents will be included but nothing will be returned.

Return value

The actual name of the class to call, or NULL if the class does not exist.

2 calls to ctools_plugin_get_class()
ctools_export_ui_get_handler in includes/export-ui.inc
Get the class to handle creating a list of exportable items.
ctools_plugin_load_class in includes/plugins.inc
Load a plugin and get a class name from it, returning success only if the class exists.

File

includes/plugins.inc, line 739
Contains routines to organize and load plugins. It allows a special variation of the hook system so that plugins can be kept in separate .inc files, and can be either loaded all at once or loaded only when necessary.

Code

function ctools_plugin_get_class($plugin_definition, $class_name, $abstract = FALSE) {

  // If cached the .inc file may not have been loaded. require_once is quite safe
  // and fast so it's okay to keep calling it.
  if (isset($plugin_definition['file'])) {

    // Plugins that are loaded from info files have the info file as
    // $plugin['file'].  Don't try to run those.
    $info = ctools_plugin_get_info($plugin_definition['plugin module'], $plugin_definition['plugin type']);
    if (empty($info['info file'])) {
      require_once './' . $plugin_definition['path'] . '/' . $plugin_definition['file'];
    }
  }
  if (!isset($plugin_definition[$class_name])) {
    return;
  }
  if (is_array($plugin_definition[$class_name]) && isset($plugin_definition[$class_name]['class'])) {
    if (isset($plugin_definition[$class_name]['parent'])) {

      // Make sure parents are included.
      // TODO parent-loading needs to be better documented; the 'parent' designated
      // on the plugin actually corresponds not to the name of the parent CLASS,
      // but the name of the parent PLUGIN (and it then loads loads whatever
      // class is in the same $class_name slot). Initially unintuitive.
      ctools_plugin_load_class($plugin_definition['plugin module'], $plugin_definition['plugin type'], $plugin_definition[$class_name]['parent'], $class_name);
    }
    $class = $plugin_definition[$class_name]['class'];
    if (isset($plugin_definition[$class_name]['file'])) {
      $file = $plugin_definition[$class_name]['file'];
      if (isset($plugin_definition[$class_name]['path'])) {
        $file = $plugin_definition[$class_name]['path'] . '/' . $file;
      }
      require_once './' . $file;
    }
  }
  else {
    $class = $plugin_definition[$class_name];
  }

  // If we didn't explicitly include a file above, try autoloading a file
  // based on the class' name.
  if (!isset($file) && file_exists($plugin_definition['path'] . "/{$class}.class.php")) {
    require_once $plugin_definition['path'] . "/{$class}.class.php";
  }
  if (class_exists($class) && (empty($plugin_definition[$class_name]['abstract']) || $abstract)) {
    return $class;
  }
}