function ctools_context_keyword_substitute

Perform keyword and context substitutions.

Parameters

string $string: The string in which to replace keywords.

array $keywords: Array of keyword-replacement pairs.

array $contexts:

array $converter_options: Options to pass on to ctools_context_convert_context(), defaults to an empty array.

Return value

string The returned string, with substitutions performed.

6 calls to ctools_context_keyword_substitute()
CtoolsContextKeywordsSubstitutionTestCase::testKeywordsSubstitution in tests/context.test
Test the keyword substitution.
ctools_content_render in includes/content.inc
Get the content from a given content type.
ctools_custom_content_type_render in plugins/content_types/custom/custom.inc
Output function for the 'custom' content type. Outputs a custom based on the module and delta supplied in the configuration.
ctools_entity_field_content_type_substitute_keywords in plugins/content_types/entity_context/entity_field.inc
Replace context keywords.
page_manager_http_response_render in page_manager/plugins/task_handlers/http_response.inc

... See full list

File

includes/context.inc, line 1025

Code

function ctools_context_keyword_substitute($string, $keywords, $contexts, array $converter_options = array()) {
    // Ensure a default keyword exists:
    $keywords['%%'] = '%';
    // Match contexts to the base keywords:
    $context_keywords = array();
    foreach ($contexts as $context) {
        if (isset($context->keyword)) {
            $context_keywords[$context->keyword] = $context;
        }
    }
    // Look for context matches we we only have to convert known matches.
    $matches = array();
    if (preg_match_all('/%(%|[a-zA-Z0-9_-]+(?:\\:[a-zA-Z0-9_-]+)*)/us', $string, $matches)) {
        foreach ($matches[1] as $keyword) {
            // Ignore anything it finds with %%.
            if ($keyword[0] == '%') {
                continue;
            }
            // If the keyword is already set by something passed in, don't try to
            // overwrite it.
            if (array_key_exists('%' . $keyword, $keywords)) {
                continue;
            }
            // Figure out our keyword and converter, if specified.
            if (strpos($keyword, ':')) {
                list($context, $converter) = explode(':', $keyword, 2);
            }
            else {
                $context = $keyword;
                if (isset($context_keywords[$keyword])) {
                    $plugin = ctools_get_context($context_keywords[$context]->plugin);
                    // Fall back to a default converter, if specified.
                    if ($plugin && !empty($plugin['convert default'])) {
                        $converter = $plugin['convert default'];
                    }
                }
            }
            if (!isset($context_keywords[$context])) {
                $keywords['%' . $keyword] = '%' . $keyword;
            }
            else {
                if (empty($context_keywords[$context]) || !empty($context_keywords[$context]->empty)) {
                    $keywords['%' . $keyword] = '';
                }
                else {
                    if (!empty($converter)) {
                        $keywords['%' . $keyword] = ctools_context_convert_context($context_keywords[$context], $converter, $converter_options);
                    }
                    else {
                        $keywords['%' . $keyword] = $context_keywords[$keyword]->title;
                    }
                }
            }
        }
    }
    return strtr($string, $keywords);
}