Context-Aware Get Alias by ID

This is piggy-backing off the solution in this thread
https://community.modx.com/t/list-all-contexts-and-link-to-their-site-start-and-pull-other-settings-as-well/

I’m trying to getAliasbyId but be context-aware. I saw that we used to have an extra called aliasId that appears to have had this functionality. But it’s no longer available.

Below is a version of the solution from the previous thread. In attempts to do what I’ve described above, I’m trying to set placeholders for the resources within each context all based on the alias so that I can return the ID and possibly other information like pagetitle.

For example: I have a page called “Home” in each context that I want to set as the site_start for each context. Things that I’ve tried have all come back with the ID of that page in the web context.

<?php
$tpl = $modx->getOption('tpl',$scriptProperties,'');
$contexts = $modx->getCollection('modContext', array('key:NOT IN' => array('web', 'mgr')));
foreach ( $contexts as $context ) {
    $settings = $context->toArray();
    $c_settings = $context->getMany('ContextSettings');
    $site_start_url = '';
    foreach ( $c_settings as $setting ) {
        $settings[$setting->get('key')] = $setting->get('value');
    }
    $listContext .= $modx->getChunk($tpl,$settings);
}
return $listContext;

Quick note: I only need to set the placeholders for the ID, pagetitle, etc. I will be using them in a TPL after this loop runs.

This should get you started:

$results = array();
$tpl = $modx->newObject('modChunk');
$output = '';
$tpl->setContent("\n    <li>[[+key]]: [[+value]]</li>");
$contexts = $modx->getCollection('modContext', array('key:NOT IN' => array('web', 'mgr')));

foreach ($contexts as $context) {
    $cKey = $context->get('key');
    $settings = $context->getMany('ContextSettings');

    foreach($settings as $setting) {
        /* Getting these two separately seems to be necessary */
        $key = $setting->get('key');
        $value = $setting->get('value');
        $results[$cKey][] = array('key' => $key, 'value' => $value);
    }
}

foreach ($results as $context => $settings) {
    /** @var modChunk $tpl */
    $output .= "\n<h3>" . $context . '</h3>';
    $output .= "\n<ul>";
    foreach($settings as $fields) {
        $tpl->setCacheable(false);
        $output .=  $tpl->process($fields);
    }
    $output .= "\n</ul>";
}

return $output;

I had to guess as the output you wanted.

I used an “ad hoc” chunk, because I was too lazy to create an actual chunk. This might be faster than calling getChunk(), but using str_replace(), rather than process() or getChunk(), would be faster still. It would also be more “correct” to use an outer Tpl like the following, but it would be slower.

OuterTpl:

<h3>[[+context]]</h3>

<ul>
    [[+context_settings]]
</ul>

Thanks, Bob! But I’m looking for the actual resource settings within the context now. I’ve got the settings piece working already.

I’m not sure I understand exactly what you are trying to do. But if you are trying to query a resource based on its alias and context, then code like this should work:

<?php
$context_key = "web";
$alias = "home";
$resource = $modx->getObject('modResource', ["context_key" => $context_key, "alias" => $alias]);
if ($resource){
    $id = $resource->get("id");
    $pagetitle = $resource->get("pagetitle");
}