Make MODX Search by ID

Make the MODX Manager's search function search by ID


The Search feature at the upper left of the MODX Manager is very handy, but at this writing, it won't let you search by ID. In this article we'll look at a simple change that will fix that.

MODX logo

The Problem

Searching for a MODX object (resource, plugin, snippet, etc.) is easy enough if you know its name. If you have spent much time looking at the MODX error log, though, you know that in error messages, the objects are identified by ID, like this (all on one line):

[2018-01-13 15:10:26] (ERROR @
    \core\cache\includes\
    elements\modsnippet\46.include.cache.php :
    2) PHP warning: fopen(somefile):
    failed to open stream: No such file or directory

This error message tells us that the problem is with a snippet, and that the ID of the snippet is 46, but there's no clue about which snippet that is.

If you have a large site, it can be a real pain to wade through the appropriate tree to find the snippet with that ID. This is especially true for objects that are in categories or subfolders that are not showing in the tree. You can edit the \core\cache\includes\elements\modsnippet\46.include.cache.php file on the File tree in the Manager and hope that the name of the object is in the comments, or that you can identify it by looking at the code, but it's not very convenient. Even if you get the name that way, you still have to look for it in the Snippets section of the Element tree.

There's also a problem when you know the ID of a resource, but not its pagetitle. You could also be looking for a user with a particular user ID or for any other MODX object that you know the ID of but not the name.


The Solution

Let me say first that this solution requires you to modify the MODX core code and that it will be overwritten by upgrades to MODX. If you've been reading my Blog or my Forum posts, you know that I've always strongly recommended against any modifications to the core code. This fix is so simple, though, and so unlikely to cause any serious problems that I'm making an exception. It requires some simple changes to just one file. I will be submitting a pull request to add it to the core code, but it may be rejected, and even if it's accepted, it may be a while before it appears in a released version of MODX. The worst that could happen after making the changes suggested here is that the Search box in the Manager won't work, and if you've backed up the one file we're modifying, you can restore it easily.

The first step is to locate the file you need to change. Be careful about the path as there is more than one file with this filename in the MODX core. The correct file is this one:

core\model\modx\processors\search\search.class.php

If you have moved, and/or renamed the core directory, the first part of the path will be slightly different, but the rest will be the same. If your moved/renamed core directory doesn't appear in the File tree in the Manager, see the previous article.

Before doing any editing, open the file, and copy the contents somewhere safe. I like to create a new file in the same directory with a .bak suffix. In this case, that would be search.class.php.bak.

Next, in the original file, find the functions that do the searching by searching for the phrase function search. You should find searchResources, searchPlugins, searchSnippets. etc. You may not want to change all of them. I just modified the functions for resources, plugins, snippets, and users, but you're free to modify whichever sections you think will meet your needs. The change is the same for each section. We'll do the searchSnippets() function as an example.

Inside the function (searchSnippets in our example), find the part that starts with $c->where. Here's that section in the searchSnippets function:

$c->where(array(
    'name:LIKE' => '%' . $this->query . '%',
    'OR:description:LIKE' => '%' . $this->query .'%',
));

The trick is to add the following line to the end of the array:

'OR:id:=' => $this->query,

After adding the line, the $c->where section would look like this:

$c->where(array(
    'name:LIKE' => '%' . $this->query . '%',
    'OR:description:LIKE' => '%' . $this->query .'%',
    'OR:id:=' => $this->query,
));

Notice there are no changes except the insertion of that line. The $this->query part is whatever is entered in the search input. All we're doing is telling MODX that we also want snippets where the id field matches the search input.

Add that same line to the end of the array in the $c->where code for every function where you want to be able to search by ID. The $c->where code is different for various options, but all you need to do is add our line to the end of the list of conditions.


Performance

The line can actually go anywhere in the $c->where array, but you need to modify the code if it's not at the bottom. The closer it is to the top, the sooner the ID results will show up (though the time difference may be very small, depending on the size of your site). The first line of the $c->where() code should *not* contain OR:, and every other line must begin with OR:. Our new line could, for example, be the first line. If you search most often by ID, you might want to put it at the top of the array as shown below so that the ID search results will show up a little faster.

$c->where(array(
    'id:=' => $this->query,
    'OR:name:LIKE' => '%' . $this->query . '%',
    'OR:description:LIKE' => '%' . $this->query .'%',
));

Another option would be to make it the second line so it would follow the name search, like this:

$c->where(array(
    'name:LIKE' => '%' . $this->query . '%',
    'OR:id:=' => $this->query,
    'OR:description:LIKE' => '%' . $this->query .'%',
));

Wrapping Up

Don't forget to save the file after finishing your edits. It's not a bad idea to make a copy of your modified file (I call it search.class.php.new). That will make it easy to re-do your changes if they get overwritten by a MODX upgrade.


Coming Up

In the next article, we'll see a simple way to hide the time field when date-type TVs are edited in 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)