function patterns_get_pattern_details in Patterns 6.2
Same name and namespace in other branches
- 6 patterns.module \patterns_get_pattern_details()
Return an array with detailed information about the pattern
3 calls to patterns_get_pattern_details()
- patterns_execute_pattern_batch in ./
patterns.module - patterns_execute_pattern_drush in ./
patterns.drush.inc - patterns_execute_pattern_drushbatch in ./
patterns.drush.inc - This is a fork of patterns_execute_patterns_batch(), and will be adapted to use the drush batch api instead @todo (evergreen todo): make sure this is always in sync with patterns_execute_pattern_batch().
File
- ./
patterns.module, line 1861 - Enables extremely simple adding/removing features to your site with minimal to no configuration
Code
function patterns_get_pattern_details($pattern, $recursive = FALSE, &$pids = array()) {
// prevent infinite recursion
// Disabled! Infinite recursion is possible!
// This allows the same pattern to be re-executed with different parameters
// TODO: detect recursion, and protect users from it
// if (in_array($pattern->pid, $pids)) return array();
$pids[$pattern->pid] = $pattern->pid;
$actions = !empty($pattern->pattern['actions']) ? $pattern->pattern['actions'] : array();
$modules = !empty($pattern->pattern['modules']) ? $pattern->pattern['modules'] : array();
$patterns[$pattern->pid] = (array) $pattern;
$patterns[$pattern->pid] = array_merge($patterns[$pattern->pid], $patterns[$pattern->pid]['pattern']['info']);
unset($patterns[$pattern->pid]['pattern']);
if ($recursive) {
$result = array(
'modules' => $modules,
'info' => $patterns,
);
foreach ($actions as $key => $action) {
if ($action['tag'] == 'pattern') {
// determine pattern name
if (!empty($action['value'])) {
$name = $action['value'];
}
elseif (!empty($action['name'])) {
$name = $action['name'];
}
if (!($p = patterns_get_pattern($name))) {
// just give a warning and try to continue
drupal_set_message(t('Action #%key in %file: Pattern %pattern not found.<br>Pattern execution will try to continue without it.', array(
'%key' => $key + 1,
'%file' => $pattern->title,
'%pattern' => $name,
)), 'warning');
continue;
}
// Decide if sub-pattern needs to be run based on the mode defined within the pattern or selected in UI at the time of form submission
// @TODO: UI setting should be able to override a setting defined within the pattern
$modes = array(
'first-update',
'always',
'update',
'first',
'never',
);
if (!empty($action['run']) && in_array($action['run'], $modes)) {
$mode = $action['run'];
}
else {
$mode = $pattern->subpatterns_run_mode;
}
switch ($mode) {
case 'never':
// don't run sub-pattern
drupal_set_message(t('Action #%key in %file: Pattern %pattern not ran because the pattern was set to be skipped.', array(
'%key' => $key + 1,
'%file' => $pattern->title,
'%pattern' => $name,
)), 'status');
continue 2;
break;
case 'first':
// Only run on first run
if ($p->status) {
drupal_set_message(t('Action #%key in %file: Pattern %pattern not ran because the pattern was set to execute only on the first run.', array(
'%key' => $key + 1,
'%file' => $pattern->title,
'%pattern' => $name,
)), 'status');
continue 2;
}
break;
case 'update':
// Only run on pattern update
if ($p->enabled >= $p->updated) {
drupal_set_message(t('Action #%key in %file: Pattern %pattern not ran because the pattern was set to execute only on pattern update.', array(
'%key' => $key + 1,
'%file' => $pattern->title,
'%pattern' => $name,
)), 'status');
continue 2;
}
break;
case 'first-update':
// Only run on first run or pattern update
if ($p->status && $p->enabled >= $p->updated) {
drupal_set_message(t('Action #%key in %file: Pattern %pattern not ran because the pattern was set to execute only on first run or update.', array(
'%key' => $key + 1,
'%file' => $pattern->title,
'%pattern' => $name,
)), 'status');
continue 2;
}
break;
case 'always':
default:
// Run always
break;
}
$a = patterns_get_pattern_details($p, TRUE, $pids);
if (is_array($a) && empty($a)) {
// An empty array is returned on infinite recursion detection
drupal_set_message(t('Action #%key in %file: Infinite recursion detected while attempting to run pattern %pattern.<br>Pattern execution will try to continue without it.', array(
'%key' => $key + 1,
'%file' => $pattern->title,
'%pattern' => $name,
)), 'warning');
continue;
}
// we replace for tokens in the generated pattern
// this is just a proof of concept, so far
if (!empty($action['parameters'])) {
$tokens = array_keys($action['parameters']);
$values = array_values($action['parameters']);
// give tokens their delimiters
foreach ($tokens as &$token) {
$token = "__" . $token . "__";
}
$a = patterns_array_map('str_replace', $a, array(
$tokens,
$values,
));
}
// array_merge doesn't preserve numeric array keys
// so we handle 'info' separately
$info = $result['info'];
$result = array_merge_recursive($result, $a);
$result['info'] = $info + $a['info'];
}
else {
$result['actions'][] = $action;
$result['actions_map'][] = array(
'pid' => $pattern->pid,
'index' => $key,
);
}
}
$result['modules'] = array_merge(array_unique($result['modules']));
// Remove pid from recursion stack
//unset($pids[$pattern->pid]);
return $result;
}
// Remove pid from recursion stack
//unset($pids[$pattern->pid]);
return array(
'actions' => $actions,
'modules' => $modules,
'info' => $patterns,
);
}