You are here

function patterns_parser_should_include in Patterns 7

Same name and namespace in other branches
  1. 7.2 includes/parser/parser.inc \patterns_parser_should_include()

Determines if a pattern should be included or not. The following conditions are checked in order:

  • if a 'run' attribute is associated with the include tag
  • if $mode parameter was passed
  • the default include mode from system variables

Parameters

mixed $pattern the including pattern, identified by its id or its name, or: the full object as loaded from the database. If the including

array $include the include tag from a pattern:

mixed $mode a valid mode for sub-patterns execution:

2 calls to patterns_parser_should_include()
patterns_parser_build_include_options in includes/parser/parser.inc
patterns_parser_should_include_local in includes/parser/parser.inc
Determines whether an pattern should be included based on the local configuration (the pattern code itself).

File

includes/parser/parser.inc, line 632

Code

function patterns_parser_should_include($pattern = NULL, $options = array()) {
  $options = _patterns_parser_merge_default_include_options($options);
  $pattern = is_null($pattern) ? $options['pattern'] : $pattern;
  $updated = $options['updated'];
  $enabled = $options['enabled'];
  $verbose = $options['verbose'];
  $mode = $options['mode'];

  // If we set $mode programmatically, that has the highest priority
  // otherwise fallback on default configuration
  if (!patterns_parser_is_valid_include_mode($mode)) {
    $mode = variable_get('patterns_default_include_mode', PATTERNS_INCLUDE_NEVER);
  }

  // Do we want to include it into a specific pattern?
  // If so we need to check its status from the database
  if (!is_null($pattern)) {
    $p = _patterns_db_get_pattern($pattern);
    if ($p) {
      $updated = $p->updated >= $p->enabled ? TRUE : FALSE;
      $enabled = $p->status == PATTERNS_STATUS_ENABLED ? TRUE : FALSE;
    }
  }
  switch ($mode) {
    case PATTERNS_INCLUDE_ALWAYS:
      break;
    case PATTERNS_INCLUDE_ATTACHED:

      // This option is checked by patterns_parser_should_include_local
      break;
    case PATTERNS_INCLUDE_FIRSTRUN:

      // Only run on first run.
      if ($enabled) {
        if ($verbose) {
          drupal_set_message(t('A pattern was not included because the pattern was set to execute only on the first run.'), 'status');
        }
        return FALSE;
      }
      break;
    case PATTERNS_INCLUDE_UPDATE:

      // Only run on pattern update.
      if (!$updated) {
        if ($verbose) {
          drupal_set_message(t('A pattern was not included because the pattern was set to execute only on pattern update.'), 'status');
        }
        return FALSE;
      }
      break;
    case PATTERNS_INCLUDE_FIRSTRUN_OR_UPDATE:

      // Only run on first run or pattern update.
      if (!$enabled || !$updated) {
        if ($verbose) {
          drupal_set_message(t('A pattern was not included because the pattern was set to execute only on first run or update.'), 'status');
        }
        return FALSE;
      }
      break;
    case PATTERNS_INCLUDE_NEVER:
    default:
      if ($verbose) {
        drupal_set_message(t('A pattern was not included because that is forbidden by the current configuration.'), 'status');
      }
      return FALSE;
      break;
  }
  return TRUE;
}