My supervisor told be that I need to find a better way of creating links in htmlarea. He said that some of our clients will struggle to type in the correct path. Yes he thinks some of our clients are that stupid so I started looking at ajax autocomplete in 4.7. This is what I have come up with.
In the htmlarea.module file I added this into the menu hook, after the "admin/settings/htmlarea/custom" item.
$items[] = array('path' => 'htmlarea/autocomplete',
'title' => t('node autocomplete'),
'callback' => 'htmlarea_autocomplete_link',
'access' => 1,
'type' => MENU_CALLBACK);
I add this to the end of the htmlarea.module file.
function htmlarea_autocomplete_link($string) {
$matches = array();
$string = '%' . trim($string) . '%';
$result = db_query_range("SELECT nid, title FROM {node} WHERE title LIKE '%s'", $string, 0, 10);
while ($nodes = db_fetch_object($result)) {
$matches['node/' . $nodes->nid] = check_plain($nodes->title) . ' [' . 'node/' . $nodes->nid . ']';
}
print drupal_to_js($matches);
exit();
}
Then I added this into the head of my htmlarea/xinha-nightly/popups/link.html file.
<style type="text/css" media="all">
@import "/path_to_site/misc/drupal.css";
.auto {
background: #fff;
}
</style>
<script type="text/javascript" src="/path_to_site/misc/drupal.js"></script>
<script type="text/javascript" src="/path_to_site/misc/autocomplete.js"></script>
Then I change the url form item to this.
<td><span class="auto"><input type="text" id="f_href" style="width: 100%" class="form-autocomplete" /></span><input class="autocomplete" type="hidden" id="f_href-autocomplete" value="http://example.com/path_to_site/?q=htmlarea/autocomplete" disabled="disabled" /></td>
I had to add the span to the text field and style it white else the background come through gray.
If you are going to try this don't for get the refresh your menu cache by going to admin/menu and refreshing the page.
Its a pity the link file is not a php file, because then we could use drupal's $path vars instead of manually putting in the path to the site.
Hope this proves useful to some body else.
Comments
Comment #1
gordon commentedThis patch doesn't really apply to htmlarea as the main part of this is not even a part of drupal, but a part of Xinha.
Comment #2
NaX commentedYes this is not a patch but a hack.
BUT...
The way the image upload module integration works is by registering a Plug-in with Xinha that points to a php page inside the htmlarea module.
What we have here is a starting point to something similar. We could create a drupal createlink plug-in for htmlarea that gets added into Xinha and admins can then choose what they want to use. The default Xinha link system or a drupalized link plug-in with some web 2.0 features that integrate into drupal.
I also updated my autocomplete function to work with the path module.
I realized I forgot to explain what it does. It searches node titles. That way you can search for links via the title. It works the same way as the Authored by ajax autocomplete.