This may be useful in other places also, but I'll start small with a patch to
links_load_link_for_node()
to allow the caller to define the key used for the returned array. A bit like taxonomy (and others) do these days. The change ($indexedby parameter) looks like:

function links_load_links_for_node($nid, $module='links', $lid=0, $firstonly=FALSE, $indexedby = 'url') {
...
      $links[$row[$indexedby]] = $row;
...
  return $links;
}

So instead of a count-indexed array being returned, I get something that's a bit easier to check for existing urls in etc. Set it to NULL and you'll get the old behaviour.

It's a trivial change (so far) but I think it's a clean improvement, and better than sorting it later.
Anyone can suggest a different 'default' key to index on if you'd care.

In the meantime, I'm tonight lookng at making the links_related.module actually work (again?) and fix up that edit interface... more suggestions to come.

CommentFileSizeAuthor
links_indexedby.patch1.35 KBdman

Comments

dman’s picture

I see this would help clean up the code here

function links_save_links_for_node(&$node, $module='links_links', $global=FALSE) {
  $oldlinks_byrow =& links_load_links_for_node($node->nid, $module);
  // Need it arranged by link ID, for lookup
  $oldlinks = array();
  for ($i=0; $i<count($oldlinks_byrow); $i++) {
    $oldlinks[$oldlinks_byrow[$i]['lid']] = $oldlinks_byrow[$i];
  }

as well...
... actually I see that because it's using a manual for loop instead of a natural foreach something might break a little. ah well. Fixing it will be better.

dman’s picture

Status: Needs review » Closed (fixed)

This can be closed as it's being rolled into a larger update
I encountered a few problems with code elsewhere expecting number-indexed results (always retrieving $links[0] instead of using, eg, array_pop($links) ) so I've set the default behaviour to be the same as legacy, but this feature can be taken advantage of in a few places.