Fetching the pagetitle of the resource a weblink points to

I am wanting to use an If call within a getResources output to detect if the resource is a weblink and, if so, it should output code which references the pagetitle of the resource that is being redirected to.

Something along these lines:

[[!If? &subject=`[[+class_key]]` &operator=`EQ` &operand=`modWebLink` &then=`Please see: <a href="[[~[[+id(of resource weblink points to)]]]]">[[+pagetitle(of resource weblink points to)]]</a>` &else=`<a href="[[~[[+id]]]]">[[+pagetitle]]</a>`]]

Any suggestions as to how to acheive this?

Not sure if there is an easy way to achieve this, but here is a convoluted one:

[[+class_key:is=`modWebLink`:then=`Please see: <a href="[[+content]]">[[#[[+content:ContentID]].pagetitle]]</a>`:else=`<a href="[[~[[+id]]]]">[[+pagetitle]]</a>`]]

If needs the extra fastField to work and you have to create a new snippet ContentID with the code

<?php
$resourceID = $modx->findResource($input);
return $resourceID;

to get the ID of the resource from the URL.

not sure, but maybe this is possible with pdoResources and the &useWeblinkUrl property
pdoResources / Snippets / pdoTools / docs.modx.pro

I think that would do the opposite of what’s needed, in the sense that it generates the url of the weblink itself, which I don’t really need. I may come up with a workaround which requires an extra tv for the weblink which lets me manually input the target resource’s pagetitle.

Thanks, but this doesn’t work for me. I just get empty <a> tags and no title on the weblinks.

Did you use &includeContent=`1` in your call to getResources?

[[getResources?
	&includeContent=`1`
	...
]]

Yes, but still no output sadly.

Have come up with a bit of a hack for this - I’ve added the title of the page I’m linking to in the Menu Title field of the weblink and then referenced this back in the chunk used by getResources. Saves me creating another tv and, as the resources concerned never appear in menus, it will not cause any issues elsewhere.

Not exactly elegant and I’d love to find a better solution, but it works for what I need for now…

I remembered this thread, so I think the snippet ContentID should be

<?php
$search_pattern="/\[\[~(\d+)\]\]/";
if (preg_match($search_pattern, $input, $match)){
    return $match[1];
} else {
    return $input;
}

but it is still not a clean solution as it creates a lot of “Could not find snippet” errors for all the non weblink resources.