Yearly Archives: 2015

jquery: how to unwrap text if a link href is corrupt (“un-link”)

Sometimes you may need to unwrap some text from an html element – with jQuery you can do it even after the document is already rendered.

Over the years the free JavaScript library jQuery has become very popular among both web designers and web developers. For some it es something like the Swiss Army Knife of webdesign and front end development. And for me it came in very handy when I had to deal with pre-formated html documents that I needed to clean up.

In these documents I have lists of items and basically all should have a working link wrapped around an image. But of course some items don’t have a link – or actually the link is there, but it’s pointing to a page that does not exists. Others items don’t even have an image. Since the html code is as it is, I have no way to check it or alter it on the sever, before it’s rendered, using a php snippet – so I have to do the clean up on the client side through a small jQuery code snippet instead.

This is the actual html code I’m refering to: two entries – first one with a corrupt link (“/node/EMPTY”) and a corrupt image (“/images/EMPTY-thumb.JPG”). The second entry has a working link (“/node/NUMBER”) and an image (“/images/NUMBER-thumb.JPG”). You will notice there is actually a url inside the link tag and an image path inside the image tag – but both are corrupt and lead nowhere. So I need to unwrap the contents of those corrupt links with jQuery: first look into the actual link, check if it’s corrupt – and eventually throw it over board.

<div>
<a href="/node/">
<div>
<img src="/images/-thumb.JPG"/>
</div>
<div>term A</div>
</a>
</div>
<div>
<a href="/node/64207">
<div>
<img src="/images/64207-thumb.JPG"/>
</div>
<div>term B</div>
</a>
</div>

The following code is the jQuery function I originally used to unwrap the link tag “a”. It does actually remove the corrupt link on the first item, but it also removes the link from the second item, of any other following item, if any corrupt link tag is found:

// remove corrupt links
$("a").each(function() { 
var href = $(this).attr("href");
if(href == '/node/') {
$('a').contents().unwrap();
}
});

So instead of unwrapping all the following links, the function should just unwrap ‘this’ link tag that is actually corrupt. So instead of this:

$('a').contents().unwrap();

it needs to be this:

$(this).contents().unwrap();

So in context the function looks now like this:

// remove corrupt links
$("a").each(function() {
var href = $(this).attr("href");
if(href == '/node/') {
$(this).contents().unwrap();
}
});

What it does, is:

  1. loop through all links
  2. put the content of href into the variable ‘href’
  3. check if the link consists of ‘/node/’ only
  4. unwrap the contents of this link – leave the previously linked contents an-linked

…so with this little twist the above jquery snippet actually only unwraps contents from the corrupt links – so in my case the link is removed from the picture.

…done.

PS: The above use case is quite a special case – you could however also use this method to unwrap or re-wrap text from inside a div container (or whatever container).

See also:

related links:

Scald: theming atom reference images – where to find image path

For a Drupal 7 project I use the image and assets management module Scald. I find the handling of module rather comfortable: it lets the editor choose so called media atom from a media pool and insert them into CKEditor either by drag’n’drop, or by using the ‘insert’ funtionality of Scald.

The module comes along with a field – the Atom Reference – which half works like an image field, half like an Entity Reference field. But finding the actualy image path first was a bit tricky: when printing the node array, you’ll just get the SID for the referenced image, but no details – they’re loaded through the scald function ‘scald_atom_load’.

This gets the SID from the node’s array:

$img = $vars['node']->field_teaser_image['und'][0]['sid'];

SImilar to ‘entity_load’  this fetches the atom reference’s object:

$atom = scald_atom_load($img);

This is how I then load the title of the atom reference image inside template.php:

$vars['img_title'] = $atom->title;

And the actual filename is inside another object base_entity:

$vars['img_filename'] = $atom->base_entity->filename;

Found the solutions through this scald issue. Took me a while – hope this may help somebody else some day.

iPhoto: How to play a slideshow in random order

Scenario: In iPhoto you have a folder full of photos that you want to turn into a slideshow – only you would want to play the images in random order. But: there is actually no ‘preset‘ for “random order” or “shuffle mode” in the iPhoto preferences panel – and you ask yourself, if it is actually possible, to play an iPhoto slideshow in random order at all?!

This may first occur a bit strange, but it does actually make perfect sense. Let me explain: you probably don’t really want to have your images played in random order all the time. Instead the random play option is available on demand or actually on play – which means  you can switch to random order (“shuffle order”) while the slideshow is actually playing. Here is how:

How to play an iPhoto slideshow in random order

First you obviously have to have a slideshow you want to play in random order. So in the iPhoto sidebar under ‘slideshows’ you either right-click and choose “New Slideshow” or go to “File > New Slideshow” from to menu bar. Add some picture you want to play by draggin and dropping them from any of the image overviews. Then press ‘Play’.

While the slideshow is playing, move the mouse pointer to make the slideshow controls appear. Then click the settings button, that looks like a cog wheel icon, next to the close [X] button:

iphoto slideshow random order - shuffle preferences

random order slideshow preferences in iPhoto

Clicking the settings button in play mode will then open an extended preferences panel, featuring an additional option “Shuffle slide order” in the lower third of the panel:

iPhoto slideshow random order option

iPhoto’s random order slideshow option “shuffle slide order”

Ticking (Checking/unchecking) the option “Shuffle slide order” will toggle the shuffle mode.

You can find a detailed description on this page “Change slideshow settings“at support.apple.com – it’s written for iPhoto 9.5 but should also apply for iPhoto 9.6 and other versions as well.

iphoto slideshow manual orderIf you instead want to determine the order of the slideshow manually, you can drag and drop the images in the order you prefer at the top image bar.

Hope this short guide helped a bit. Cheers!

Drupal: CKEditor toolbar inactive for other roles than ‘administrator’

I suppose it probably is some permissions problem.

screen-shot-2015-03-27

to be continued…

CKEditor for Drupal 7 Installation Process (Open Source Version)
http://docs.cksource.com/CKEditor_for_Drupal/Open_Source/Drupal_7/Installation

How To Set Up the CKEditor Module In Drupal
http://tomandcrystal.com/how_to_set_up_the_ckeditor_module_in_drupal

Drupal: SCALD modal window loading problem [solved]

The SCALD modal window ‘loading’ wasn’t loading. It took a couple of days (hours) and an embarassing long discussion on drupal.org to find the error – which was not an error. It was ‘just’ a problem of re-declaring something, that was already declared before.

The short version:
“The WYSIWYG module was detected. Using both modules at the same time may cause problems. It is recommended to turn the WYSIWYG module off (click here to disable).”
[reading (and understanding) a warning on the CKEditor profile page can actually be useful!]

The long version: …to be continued…

The documentation of the bug hunt: https://www.drupal.org/node/2454845

removing empty nodes with jQuery

Sometimes you have empty html tags on your code tree and no matter what you try in your cms / php, you can’t avoid them to appear in the rendered html.

But even then you can remove empty instances through some compact jQuery snippets – here are some examples that show how to remove empty links and empty image tags from the html document…

This is how to remove an empty image tag, that generates an error:

 $("img").error(function(){
     $(this).remove();
 });

This is how to remove a link tag that generates an error:

 $("a").error(function(){
     $(this).contents().unwrap();
 });

This is how to hide the parent tag, if the child tag “inscriptions” is empty:

 $("inscriptions:empty").parent().hide();

This is how to remove ’empty’ links that point to a directory instead of a page:

 $("a").each(function() {
     var href = $(this).attr("href");
     if(href == '/node/') { // or anything else you want to remove...
         $(this).contents().unwrap();
     }
 });

This is how to remove ’empty’ image tags that point to non-existing images:

 $("img").each(function() {
     var src = $(this).attr("src");
     if(src == '/images/-thumb.JPG') { // or anything else you want to remove...
         $(this).remove();
     }
 });

See also:

And here’s some related links:

Drupal: cancel button on edit node page

Are you missing a Cancel Button in your Drupal installation? Great. I know the feeling. But as they say: there’s a module for that. For many Drupal related tasks there is already a module available at drupal.org. But some essential tasks can be so ‘small’ that the related module would be even smaller – documenting and uploading it seems to be almost a waste of time. And since writing your own module in Drupal is actually not that difficult, finding a solution for such ‘small tasks’ can be as easy as copy-paste-save-upload. And this also applies to the missing cancel button.

When I needed a cancel button for one project I was working on, I ran into exactly this situation. Or maybe ‘situation’ is to big a word for this – anyways. I needed to add a cancel button to the “new node” and “edit node” screens for all pages. You may think Why not just leave the page instead of clicking a cancel button? And I must agree: from a functional perspective such a button would be redundant.  But from a UX perspective it may very well make sense to have that button: I actually only needed that extra button because the client I worked for was used to having a cancel button from his previous CMS. He was a bit irritated that there was no cancel button in the drupal installation I developed for him. Also telling him “Just leave the page” did not help.

Instead a quick search helped. I found this little snippet from Pascal Duez that does just that: adding a custom cancel button to the “new node” and “edit node” screen of all pages. That was exactly what I was looking for. You can find the module on gihub right here: gist.github.com/pascalduez/1888373

This is what the “new node” and “edit node” screen looks like when the module is activated:

Drupal Cancel Button Example Screenshot

custom cancel-button for Drupal

There is also a slightly more complex, or actually more ‘handy’ version of that script. It also forwards the user to a different, useful page, according to the context in which the cancel button was clicked:

” If the “Cancel” button is clicked, the user is being redirected the following way:

  1. If a “destination” paramter is set, this is used
  2. If no destination parameter is set and we’re in an edit form, the user is being redirected to the node display
  3. Otherwise the user is being redirected to the frontpage “

You can find the slightly more complex module from here:
julian.pustkuchen.com/en/drupal-7-snippet-add-cancel-button-content-edit-create-forms

drupal 7 admin toolbar disappearing [fix]

I recently had the problem, that my admin toolbar would be disappearing randomly. This of course can be a major problem – how do you want to administer your drupal installation when there is no toolbar? Only emptying the cache helped – so having the url to empty the cache at hand can be – handy.

If you happen to have the development module installed (which you should have) then the following path with flush the cache:

/devel/cache/clear

but also the following path lets you empty the cache manually:

/admin_menu/flush-cache

In my case this problem seemed to be a cache related problem. So emptying the cache helped. Luckily I found an easy fix for the problem over at drupal.org – so I didn’t have to empty the cache constantly. Here’s the details:

at the end of settings.php add this:

// admin toolbar fix
 $conf['admin_menu_cache_client'] = FALSE;

i found the quick fix for this problem over here: “Admin menu disappears (randomly), needs either cache to be emptied on every page or page refresh several times”
https://www.drupal.org/node/442560

and the fix was then right here:
https://www.drupal.org/node/442560#comment-2692220