Find My Symlinks II

Displaying all symlinks on a MODX site


In the last article, we looked at how to find all the symlinks pointing to a particular resource. In this one, we'll take a look at code to find and display *all* symlinks on a MODX site.

MODX logo

Finding all symlinks on a site and displaying them requires some tricky code. What we really want is a list of all resources that have symlinks, followed by a list of their symlinks. Since we have to find the symlinks first, our code is kind of inside out. This is a good example of a strategy that's often necessary: building the array of things to be displayed in one loop, then displaying them in another loop.


Pre-strategy

A little planning can save you a lot of time, especially on a complex problem. In building this code, I first created two resources, Test1 and Test2. Then I created four symlinks, two pointing to each resource. I named that symlinks like this so that I could see what resource they pointed to by looking at their pagetitles:

Symlink for Test1
Another Symlink for Test1
Symlink for Test2
Another Symlink for Test2

Strategy

First, we'll get all the symlinks on the site. They we'll loop through them. As we go, we'll create an array of original resources and their symlinks. When that first loop has finished, we'll loop through it to display each original resource, followed by its symlinks. Notice that after the first loop is finished, we use print_r(), wrapped in pre tags to return our array. It's a good strategy to display a complex array before writing the code to process it. First, we need to know that the array looks like it should and contains all the data. Second, it's really helpful in writing the code to display the data. In fact, we'll start with just the first loop.


Initial Code

Here's that first bit of code. We've used $targets as the first object in the second loop. Using $originals might have been clearer, but we'll use that variable in the second loop, and using a different variable here will help our code editor point out mistakes in the second loop.

/* Get all symlinks */
$symlinks = $modx->getCollection('modResource', array('class_key' => 'modSymLink'));

/* Initialize variables */
$output = '';
$targets = array();

/* Create our array of objects */
foreach ($symlinks as $symlink) {
    $symlinkId = $symlink->get('id');
    $targetId = $symlink->get('content');
    $targets[$targetId][$symlinkId] = $symlink->get('pagetitle');
}
return print_r($targets, true);

Here's the output of that code:

Array
(
    [2069] => Array
        (
            [4789] => Symlink for Test1
            [7053] => Another Symlink for Test1
        )

    [38] => Array
        (
            [7054] => Symlink for Test2
            [7055] => Another Symlink for Test2
        )

)

The left-most IDs are the IDs of our target (original) resources (Test1 and Test2). The other ids are the IDs of each symlink. It wouldn't have hurt to make the ID of the symlink part of the symlink's pagetitle. Then we could have made sure that the inner IDs were correct without having the check them.

You might have noticed that although we have the IDs of the original resources, we don't have the pagetitles here. That's because they are not available in the symlink object. We could have gotten them using $modx->getObject(), but there's really no good place to put them in this array without making it even more complex than it already is. We can easily get them in the display loop, and doing so will make our code easier to understand.

We've added the second loop and commented out the statement returning the array from the first loop.

The Full Code

/* Get all symlinks */
$symlinks = $modx->getCollection('modResource', array('class_key' => 'modSymLink'));

/* Initialize variables */
$output = '';
$targets = array();

/* Create our array of objects */
foreach ($symlinks as $symlink) {
    $symlinkId = $symlink->get('id');
    $targetId = $symlink->get('content');
    $targets[$targetId][$symlinkId] = $symlink->get('pagetitle');
}

/* Get all symlinks */
$symlinks = $modx->getCollection('modResource', array('class_key' => 'modSymLink'));

/* Initialize variables */
$output = '';
$targets = array();

/* Create our array of objects */
foreach ($symlinks as $symlink) {
    $symlinkId = $symlink->get('id');
    $targetId = $symlink->get('content');
    $targets[$targetId][$symlinkId] = $symlink->get('pagetitle');
}

// return '<pre> . print_r($targets, true) . . 
; /* Use $output to display the results */ foreach ($targets as $targetId => $symlinks) { $indent = '    '; /* We need to get the original object so we can get it's ID, and pagetitle */ $original = $modx->getObject('modResource', $targetId); $originalPagetitle = $original->get('pagetitle'); $originalId = $original->get('id'); $output .= "\n" . '

Original: ' . $originalPagetitle; $output .= "\n
Symlinks:"; foreach ($symlinks as $id => $pagetitle) { $output .= "\n
{$indent}" . $pagetitle . ' (' . $id . ')'; } } return $output;

Improvements

We now have a nicely formatted display of all symlinks organized under the resource objects they point to. It would be nice, though if you could click on them to edit the resources and symlinks. We'll see how to do that in the next article.


Coming Up

In the next article, we'll look at code to turn the listings of resources and symlinks into links to edit them.



Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)



Comments (0)


Please login to comment.

  (Login)