Find My Symlinks III

Provide links to edit each object in the Manager


In the last article, we looked at how to find all the symlinks on our site and display their symlinks.In this one, we'll make them links to edit each object.

MODX logo

The New Code

Here's the new code, the only changes are to the lines in the second loop that display the results.

/* 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 echo print_r($targets, true);

/* 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');
}

/* 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');

    /* New Code */
    /* Display Original */
    $output .= "\n" . '<br><br>Original: <a href="'
        . MODX_MANAGER_URL . '?a=resource/update&id=' . 
        $originalId . '">' . $originalPagetitle . 
        ' (' . $originalId . ')</a>';

    /* Display Symlinks */
    $output .= "\n<br>Symlinks:";
    foreach ($symlinks as $id => $pagetitle) {
        $output .= "\n<br>{$indent}" . '<a href = "' .
           MODX_MANAGER_URL . '?a=resource/update&id=' . 
           $id . '" > ' . $pagetitle . ' (' . 
            $id . ')</a>';
    }
}

return $output;


Improvements

The styling here could use a little work. If this were not a simple utility snippet, we'd probably make these list items and use classes and a CSS file to style them. Doing that is left as an exercise for the reader.


Coming Up

In the next article, we'll look at what to do when a dashboard widget crashes the MODX Manager.



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)