Permalink
Browse files

Code cleanup and commenting

  • Loading branch information...
1 parent 1a8270b commit 8d7a8b5f9787ca15f1b0c772f906b230107f0de6 @shakty committed Apr 21, 2012
Showing with 60 additions and 123 deletions.
  1. +60 −123 patterns.module
View
183 patterns.module
@@ -773,81 +773,39 @@ function patterns_invoke($hook, $action, &$data = NULL, $form_id = NULL, &$a = N
}
/**
- * Creates the list of modules implementing the pattern hook. Additional
- * information is provided such as the files or the forms needed by the tag. It
- * uses caching, unless a specific tag is being looked up.
- *
- * @param string $tag_name
- * (optional) If set, only information about this specific tag is returned.
- * Defaults NULL.
- * @param array $data
- * (optional) The data of the pattern. If $tag_name was set, $data must be
- * set as well. Defaults NULL.
- * @param integer $key
- * (optional) If set, only the values from that key from all modules
- * are returned. E.g. (PATTERNS_CREATE, PATTERNS_MODIFY, etc.)
- *
- * @return array $tag_modules The tag_modules index built according to the parameters.
+ * Builds an associative array of tags and modules (components).
*
- * TODO: Do we still need the $data parameter ? Probably not.
- */
-function patterns_get_tagmodules_index_old($tag_name = NULL, $data = NULL, $id = NULL) {
- static $tag_modules;
-
- // Index not yet built or $tag_name is set.
- if (!is_array($tag_modules) || $tag_name != NULL) {
- $tag_modules = array();
- // Get a list of tags and their modules.
- // TODO: Optimize: if $tag_name is set, and $tag_modules is not empty, look
- // for only the modules that we have seen before implement the tag.
- foreach (module_implements('patterns') as $module) {
- $tags = module_invoke($module, 'patterns', $data);
- foreach ($tags as $tag => $value) {
- // Add the reference to the module within the information which each
- // module is already returning
- $value['module'] = $module;
- $tag_modules[$tag] = $value;
- }
- }
- }
-
- if (empty($tag_name)) {
- return $tag_modules;
- }
- elseif (empty($id)) {
- return $tag_modules[$tag_name];
- }
- else {
- return $tag_modules[$tag_name][$id];
- }
-}
-
-/**
+ * Invokes hook_patterns on all modules, and returns an index of
+ * available tags and information associated with them (e.g. from which
+ * module they are handled).
+ *
+ * The index is cached, but reloading can be forced.
*
- * @param $data
- * @param $tag
- * @param $key
+ * @param array $data (optional) An associative array of data, that the
+ * components can use to build dynamic forms names. Defaults NULL.
+ * The array usually corresponds to an action in a pattern file, e.g.
+ *
+ * array('tag' => 'node',
+ * 'type' => 'article',
+ * 'title' => 'Test Article',
+ * 'body', => 'lorem ipsum ...',
+ * );
+ *
+ * @param bool $reset If TRUE, forces rebuilding the index from all
+ * the components. Defaults FALSE
+ *
+ * @return array $tagmodules Index of tag and modules
*/
function patterns_get_tagmodules_index($data = NULL, $reset = FALSE) {
$tagmodules = &drupal_static(__FUNCTION__);
// Index not yet built or forced rebuilt
if ($reset || empty($tagmodules)) {
-
$tagmodules = array();
// Get a list of tags and their modules.
foreach (module_implements('patterns') as $module) {
_patterns_add_module_to_tagmodules($module, $tagmodules, $data);
- //$tags = module_invoke($module, 'patterns', $data);
- // _patterns_add_module_to_tags($module, $tags);
- // $tagmodules = array_merge($tagmodules, $tags);
- //foreach ($tags as $tagname => $value) {
- // Add the reference to the module within the information which each
- // module is already returning
- // $value['module'] = $module;
- // $tagmodules[$tagname] = $value;
- //}
}
}
// We may need to rebuild the index just for the specific tag
@@ -856,31 +814,65 @@ function patterns_get_tagmodules_index($data = NULL, $reset = FALSE) {
$module = $tagmodules[$tag_name]['module'];
if (!empty($module)) {
_patterns_add_module_to_tagmodules($module, $tagmodules, $data);
- //$update = module_invoke($module, 'patterns', $data);
- //_patterns_add_module_to_tags($module, $update);
- //$tagmodules = array_merge($tagmodules, $update);
}
}
// All values from all tags
return $tagmodules;
}
+/**
+ * Helper function for <patterns_get_tagmodules_index>.
+ *
+ * Call <hook_patterns> on a given module and merge the results
+ * in the tagmodules index.
+ *
+ * @param mixed $module A string representing a module (component) name
+ * @param array $tagmodules An array containing the tagmodules index
+ * @param array $data (optional) An associative array of data, that the
+ * components can use to build dynamic forms names. Defaults NULL.
+ *
+ * @see patterns_get_tagmodules_index
+ * @see _patterns_add_module_to_tags
+ */
function _patterns_add_module_to_tagmodules($module, &$tagmodules, $data = NULL) {
$tags = module_invoke($module, 'patterns', $data);
_patterns_add_module_to_tags($module, $tags);
$tagmodules = array_merge($tagmodules, $tags);
}
+/**
+ * Helper function for <_patterns_add_module_to_tagmodules>
+ *
+ * Adds the name of the module to each tag returned from <hook_patterns>.
+ *
+ * @param mixed $module A string representing a module (component) name
+ * @param array $tags Array containing the results of the invocation of
+ * <hook_patterns> on a module.
+ */
function _patterns_add_module_to_tags($module, &$tags) {
foreach ($tags as $tagname => $value) {
$tags[$tagname]['module'] = $module;
}
}
-
-function patterns_tagmodules_filter($tagmodules, $tag, $key) {
- if (empty($tagmodules)) return FALSE;
+/**
+ * Filters out information from a tagmodules index and returns
+ * the results in a new array.
+ *
+ * Select information based on the tag (e.g. node, vocabulary, etc.),
+ * or on the key (e.g. PATTERNS_CREATE), or on a combination of both.
+ *
+ * @param array $tagmodules Associative array of tagmodules
+ * @param mixed $tag (optional) Forces to return information only
+ * from this tag. Defaults NULL.
+ * @param mixed $key (optional) Forces to return information only
+ * from the tags containing this key. Defaults NULL.
+ *
+ * @return array $tagmodules The filtered tagmodules array
+ */
+function patterns_tagmodules_filter($tagmodules, $tag = NULL, $key = NULL) {
+ if (empty($tagmodules)) return array();
// Part of tag
if (!empty($tag) && !empty($key)) {
@@ -903,61 +895,6 @@ function patterns_tagmodules_filter($tagmodules, $tag, $key) {
}
}
-function patterns_get_tagmodules_index_medium($tag_name = NULL, $data = NULL, $key = NULL) {
-
- $tag_modules = &drupal_static(__FUNCTION__);
-
- // Index not yet built
- if (empty($tag_modules)) {
-
- $tag_modules = array();
- // Get a list of tags and their modules.
- // TODO: Optimize: if $tag_name is set, and $tag_modules is not empty, look
- // for only the modules that we have seen before implement the tag.
- foreach (module_implements('patterns') as $module) {
- $tags = module_invoke($module, 'patterns', $data);
- foreach ($tags as $tag => $value) {
- // Add the reference to the module within the information which each
- // module is already returning
- $value['module'] = $module;
- $tag_modules[$tag] = $value;
- }
- }
- }
- // We may need to rebuild the index just for the specific tag
- else if (!empty($tag_name) && !empty($data)) {
- $module = $tag_modules[$tag_name]['module'];
- if (!empty($module)) {
- $tag_modules[$tag_name] = module_invoke($module, 'patterns', $data);
- }
- }
-
-
-
- // Part of tag
- if (!empty($tag_name) && !empty($key)) {
- return $tag_modules[$tag_name][$key];
- }
- // Full tag
- if (!empty($tag_name)) {
- return $tag_modules[$tag_name];
- }
-
- // All the values of key=$key from all tags
- if (!empty($key)) {
- $out = array();
- foreach ($tag_modules as $tag => $value) {
- if (isset($value[$key])) {
- $out[$value['module']][$tag] = $value[$key];
- }
- }
- return $out;
- }
-
- // All values from all tags
- return $tag_modules;
-}
-
/**
* Constructs a Pattern object with default standard values.
*

0 comments on commit 8d7a8b5

Please sign in to comment.