Wednesday, September 28, 2022

Webprofiler and settings.local.php

I didn't realise the Webprofiler module has now been spun out of Devel. Took me ages to realise that some of my local dev sites failing was because the recommended Drupal 9 snippet for settings.local.php was failing.

  1. Update the Devel module. 
  2. Then install Webprofiler module
  3. Change your relevant settings.local.php snippet to include:

// Webprofiler settings
$class_loader->addPsr4('Drupal\\webprofiler\\', [ $app_root . '/modules/contrib/webprofiler/src']);
$settings['container_base_class'] = '\Drupal\webprofiler\DependencyInjection\TraceableContainer';

Tuesday, March 06, 2018

Running Cron in Drupal 7

I'm having big snags running the Cron task in Drupal 7 via my main host.

Rather than trying to call Cron with wget or lynx (which has resulted in many failures), I found a simple structure is to call cron via Drush, for example:

drush --root=/home/cognimatic/public_html --quiet cron

Either use your cPanel interface to edit Cron tasks or follow the guide at https://www.drupal.org/docs/7/setting-up-cron-for-drupal/configuring-cron-jobs-using-the-cron-command to edit your Crontab. Just make sure Drush can be called from anywhere within your server file structure and you define the root directory (the one that will have index.php and robots.txt etc in it) for your Drupal site.

Just remember you need to have Drush running on your hosting server to use this approach - see https://www.drupal.org/node/2366283 or a previous post on this blog to help you.

Nothing's easy. If it was, everyone would do it.


BT


Monday, August 21, 2017

Installing Drush on Shared Hosting (Vidahost)

I followed the guide at https://www.drupal.org/node/2366283 to install Drush on my current host of choice (Vidahost), but couldn't get Drush to work properly. Problem #1: Couldn't find CLI. Problem #2: Drush 'master' wouldn't work with my many Drupal 7.x sites.

Here's the skinny:
  • First edit your .bashrc file that Vidahost puts in the root folder and make sure the PHP alias is pointing to the CLI version of the PHP release you are using. For Drupal 7 and 8, PHP 5.6 seems to be fine:
    • Type nano .bashrc
    • Remove any aliases to PHP in the file and add the following:
      • alias php='/usr/bin/php-5.6-cli'
    • CTRL + x to save and exit the file
    • Type source .bashrc to reload your Bash file and set up the alias.
  • Install Composer in your root folder
    • curl -sS https://getcomposer.org/installer | php
    • Leave files where they are (leaves lots of files in root, but park that OCD for a change).
  • Type nano .bash_profile to add an alias:
    • alias composer="'/usr/bin/php-5.6-cli' ~/composer.phar"
    • This adds an alias to the PHP 5.6 CLI location on Vidahost - may well work for other hosts too.
  • Save your file (ctrl-X) then run your Bash file with source .bash_profile
  • Get Drush (version 8 works with Drupal 6, 7 & 8) 
    • composer global require drush/drush:8.x
  • Update your .bash_profile with another alias
    • alias drush="~/.composer/vendor/bin/drush"
  • Reload Bash file:
    • source .bash_profile
  • Type drush to see if things are working.
Fame and fortune await (etc).

Thursday, June 26, 2014

I hate VIM

Vim to me will always mean the household cleaner: but in the geek world of sysadmin... well, I'm sure it makes beginners scream too.

Like everything there are a few basics that never seem intuitive, so always have to root them out. So here's the most basic commands for server admin heaven:

i insert mode
ESC back to command mode
:q Exit
:w Write
:qa! Quit and abandon changes


h moves the cursor one character to the left.
j moves the cursor down one line.
k moves the cursor up one line.
l moves the cursor one character to the right.
0 moves the cursor to the beginning of the line.
$ moves the cursor to the end of the line.
w move forward one word.
b move backward one word.
G move to the end of the file.
gg move to the beginning of the file.
`. move to the last edit.


d starts the delete operation.
dw will delete a word.
d0 will delete to the beginning of a line.
d$ will delete to the end of a line.
dgg will delete to the beginning of the file.
dG will delete to the end of the file.
u will undo the last operation.
Ctrl-r will redo the last undo.

Wednesday, October 02, 2013

OSX Terminal - Colours for Directory Listing

If you're used to seeing a multi-coloured display when looking at directory contents: on most *nix systems, these listings have a colour code for directories, system files etc. In Mac's OSX Terminal, it's all just plain text.

One solution is use another shell app (I frequently use the one built into Pathfinder).

However, you can also turn the colour on for Terminal by editing the ~/.bash_profile file and adding the following lines:

# Add colours to Terminal 
export CLICOLOR=1 
export LSCOLORS=GxFxCxDxBxegedabagaced

Restart Terminal.

You're welcome

Tuesday, August 28, 2012

PHP Settings for Drush in OSX

Drush has been testing me...

Faced with the Fatal error: Allowed memory size of millions of bytes exhausted (tried to allocate a couple of bytes) message on practically every conceivable Drush operation, thought it was time to invest in my dev environment to sort my Mac out...

Turns out that the php.ini file used by your local web server is different to the one Drush uses on the command line. My local development server usually runs with memory_limit=512M (although hosted sites usually run with 256M or less).

To find out which version of PHP Drush uses, drush status may help. However, which php may give you the best answer: if it's /usr/bin/php and your Drush status PHP Configuration is blank, chances are PHP is running with a miniscule amount of memory resource for CLI-issued commands. In which case you need a new php.ini file in your /etc folder to give Drush more beef. There are already default ones in this folder, so just copy one of these and edit.

cd /etc 
sudo cp php.ini.default php.ini 
sudo chmod 666 php.ini 

Then edit your new php.ini file and set memory_limit=512M in the php.ini file.

If you need more help on understanding what php.ini is all about, there's a useful article at http://drupal.org/node/207036 and explains the various tweaks to help Drupal and Drush work well. Please note, not all hosting providers will allow you to adjust php.ini, especially the cheaper, shared hosting accounts.

Thursday, March 01, 2012

Creating Web Apps in iOS

Lots of ways to make your website/application run as a full screen iOS 'app.' Google the subject and you'll be spoilt for choice.

The biggest problem I had was after I'd finished the basics: created a homepage icon, set the website to be full screen capable (etc). The front page of my web app opened full screen, but as soon as you click on a link (internal or otherwise), mobile Safari is opened for the target page and all the full screen, app-like goodness is gone.

Prevent Mobile Safari Launching

To keep the user inside your iOS 'app' (and not wandering off into the big fat interweb), embed this cheeky little script right at the end of your <body> tag.

<script>(function(a,b,c){if(c in b&&b[c]){var d,e=a.location,f=/^(a|html)$/i;a.addEventListener("click",function(a){d=a.target;while(!f.test(d.nodeName))d=d.parentNode;"href"in d&&(d.href.indexOf("http")||~d.href.indexOf(e.host))&&(a.preventDefault(),e.href=d.href)},!1)}})(document,window.navigator,"standalone")</script>

And this came from a great little discussion at gist.github.com/1042026. It works on iOS 5.

Mobile Checklist

The best checklist I've found for mobifying your site (mainly for iOS and Android) is at html5rocks.com/en/mobile/mobifying/. But the key points are:

  • Create iOS/Android homepage icons - of varying sizes.
  • Embed the relevant META tags to allow the site to render full screen.
  • Optimize your site to take advantage of HTML5 and CSS3 - back to the 90s boys and girls: think of low bandwidth for those truly mobile users.
  • Use media queries to tweak your layout for different screen sizes (and portrait/landscape orientation)
  • Ensure your site design and UI is built around a touchscreen (big, fat buttons and a vertical layout for the really small devices)
  • Keep the user within your app for as long as possible (see above for 'link' issues about preventing Mobile Safari launching).

Friday, February 24, 2012

Embedding Phone Numbers with HTML5 for iOS

Snippet to embed contact data in a web page so that an iOS device can recognise text as a phone number or as contact data. Use the 'itemprop' attribute, although you can also wrap the div in a 'vcard' class, and setting the class of the phone number span to 'tel' seemed to do funky things too:

<div itemscope>
  <span itemprop="name">Rob Carr</span>
  <a class="nowrap" href="mailto:rob@google.com" itemprop="email">rob@google.com</a>
  <span class="nowrap" itemprop="tel">+44 1234 567 890</span>
</div>

I added a 'nowrap' class (white-space:nowrap;) purely for styling and usability. When the name or phone number is clicked on, you get the iOS dialogue box:


Contact info recognition doesn't seem to work on HTML5 desktop browsers, and no idea about whether other mobile devices will recognise the 'itemprop' property.

You can wade through the Microdata spec for HTML5 at http://dev.w3.org/html5/md/, or Google for more examples. HTML5 Doctor always a good read.

Thursday, February 09, 2012

Install Drupal Remotely with SSH & Drush


  1. Connect to your server with SSH (using OSX Terminal, or Putty on Windows):
    ssh <username>@<site address>
    See en.wikipedia.org/wiki/Secure_Shell for some background on SSH.
  2. In your server root directory - preferably not the 'Public HTML' directory, WGET the Drush tarball of the latest release of Drush (currently at release 4.5):
    wget http://ftp.drupal.org/files/projects/drush-7.x-4.5.tar.gz
  3. Use TAR to extract and de-compress the archive, eg:
    tar xzvf drush-7.x-4.5.tar.gz
  4. Set up an alias to Drush
    alias drush='/myserverroot/home/drush/drush'
  5. Change working directory to Public HTML root of server
  6. Download the Drupal code
    drush dl drupal
  7. Move the download 'up' one level (the .htaccess file won't get copied in the blanket move command):
    mv drupal/* .
    mv drupal/.htaccess . 
  8. Remove the original [now empty] directory: 
    rm –r drupal
  9. Create a MySQL database (you may need to do this via your host's control panel)
    mysqladmin –u <db_user> –p create <db_name>
  10. Install the Drupal site
    drush site-install standard --account-name=admin --account-pass=<useruser_pass> --db-url=mysql://<db_user>:<db_pass>@localhost/<db_name>
  11. Fame and glory await
For more advice and a screencast, go to LevelTen's site, for a great post by Tom McCracken.

Only snag I've had with some hosts is providing enough resources for PHP/Drush to execute commands: 'Out of memory' a common problem, especially on shared hosting which may have relatively low memory. 256MB seems to be the minimum you can get away with to allocate to PHP [for Drupal/Drush] to run, so make sure when you purchase a hosting package, that you can have enough memory to play with.

If you have messed about a bit with Drush, you can check where your version is stored by running which drush on the command line. In this directory you may need to add a php.ini file that has suitable settings (eg memory_limit). Lots of issues on drupal.org to help you: Google is your friend. If you can't tweak php.ini, you may be able to tweak drushrc.php - see www.mcdruid.co.uk for a bit of helpful advice.


Thursday, October 06, 2011

Steve Jobs RIP

Inspirational Stuff

Tuesday, September 13, 2011

Create a Drupal Module Patch With Git

I thought I'd written my last bit of code over 10 years ago, but here I am struggling with all this new-fangled technology and inevitably tweaking module code that makes up the great Drupal open source empire.
A quick way to generate a patch for a module you want to tweak is using Drush and Git.

1. Download the module to your test environment with Drush. FFS don't do code development on a live site - kittens will die and you might (quite rightly) lose your job.
drush dl [module-name]

2. Navigate to directory and initialize an empty repository  
cd [module-name] 
git init

3. Add the files to new repository
git add *

4. Commit the files to the repository
git commit -m 'Initial commit'

5. Make changes to the file(s) you want to change within the module's directory.

6. Create patch - do this BEFORE committing changes  
git diff —no-prefix > patchfile.patch

7. Optionally commit your latest changes to Git  
git commit -m 'Description of what you've changed' 

Save the patch, submit it on an issue queue, set the issue status to 'Needs review' and hope that some kind person reviews it so it can be committed as a permanent feature. Fame and glory awaits.

This is taken from a post by the awesome Swedish Chef dudes at Node One. http://nodeone.se/blogg/how-to-create-a-patch-for-a-drupal-module. They talk about commits against core/contrib HEAD: not really my bag, but ignore the comments about CVS (CVS must die, etc).

Monday, June 27, 2011

Charging Battery on a Touareg

Children playing in the car, forgetful spouse... who knows. Flat battery this morning in the beast known as a VW Touareg. Automatic transmission, so no chance of bump starting this 2 tonne monster either.

A quick search of the manuals to find the battery killed off my first thought - whip it out and re-charge it. Alas it's 'very complicated and must be handled by specialist personnel'. Even my engineering prowess baulked at trying to remove it (it's under passenger seat BTW). So gave up for a while and generally pondered life.

However, VW have placed some funky little terminals in the engine bay (nearside) so you can charge it in situ. Red clip goes on the terminal under the little red cover (to left of photo); black clip goes on to the earthing stud (to the right of photo), which has a convenient lightning symbol etched onto it.

Leave it for a few hours with a meaty charger, and life is back to it's fuel-guzzling, carbon-producing self. Would be easier if a nice publisher, such as Haynes, produced a workshop manual for this. Serves us right, I suppose for buying such a large vehicle, but a necessary evil, alas.

Sunday, June 26, 2011

Downloading Massive Files with a Mac

Damn that slow broadband. I needed to download the latest version of the Apple Developer's iOS toolkit. A whopper at 4.1GB. The best I've ever got from my ISP is about 0.7M. I thought I'd click on the HTTP download link and leave it running overnight. Next morning: dismal failure at about 3GB, so no DMG file downloaded :(.

Inspired by this post, I had a go at WGETing the file instead, which should be a faster download, and automatically accommodate errors/breaks in net connection... but I had a few hurdles to clear:

  1. OSX 10.6 doesn't have wget installed. I had previously installed the wonderful Homebrew package manager, and allowed me to install wget with brew install wget. Please note that you do need a version of the OSX Developer's Tools (ie, the one on your OSX install disc) prior to installing Homebrew. Worth installing Homebrew, just to make your future life much easier.
  2. To use wget for the SDK download, you need to indicate to Apple that you have a Developer's account. To grab the cookie, install Firefox's cookie exporter, log in to the the Apple Developer site and export (save) your cookies.txt file.
  3. Navigate via Terminal to the directory you saved cookies.txt, then run the following:

    wget --cookies=on --load-cookies=cookies.txt --keep-session-cookies --save-cookies=cookies.txt https://developer.apple.com/devcenter/download.action?path=/Developer_Tools/xcode_3.2.6_and_ios_sdk_4.3__final/xcode_3.2.6_and_ios_sdk_4.3.dmg
    --2011-06-22 19:09:30-- https://developer.apple.com/devcenter/download.action?path=/Developer_Tools/xcode_3.2.6_and_ios_sdk_4.3__final/xcode_3.2.6_and_ios_sdk_4.3.dmg
  4. Sit back and make a few cups of tea, or strip your car engine, go for a long walk. Took about 5 hours, despite the download being interrupted twice. The SDK downloaded just fine and now I have the newer SDK and XCode tools installed.

Wednesday, November 17, 2010

Yahoo IMAP in Outlook 2011

Just getting to grips with Outlook for Mac. Also discovered how to set up Outlook with Yahoo Mail using IMAP. Not sure if it makes a difference but I'm using the paid Yahoo mail service.

Tools > Accounts

Add new mail account - enter [from/reply to] email address and password and clear the 'Configure automatically'

Server info
  • User name (without the @yahoo.com bit)
  • Password (should be from initial dialogue)
  • Incoming server: imap.mail.yahoo.com and check the Use SSL (should default to Port 993)
  • Outgoing server: smtp.mail.yahoo.com and check Override default port and Use SSL; set port to 465

That should be it, although to keep everyone happy, you might want to confirm your account name/password under outgoing server's 'More options'.

I actually have my email address set to something else (a 'lifetime' email forwarding alias) and Yahoo seems pretty happy with that - although that's probably another benefit of paying to use the service.

Thursday, July 01, 2010

Coding Drupal in Dreamweaver CS5

I've used big IDEs in the past: all the Drupal gurus/ninjas/whatever use Eclipse, Netbeans or something way over the top. My big project programming days are well and truly over - kicked the arse out of it over the years in BASIC, Pascal, FORTRAN, CORAL, ADA, Java and C++. When I started using Drupal to build websites, my aim was to avoid touching its underlying language, PHP.  Managed that for 2 years, but recently have had to geek out to do a few cheeky things, either in the theming layer or some simple modules. My knowledge of PHP, jQuery and the Drupal API is pretty minimal, so need the following to write code:
  • Code hints (for PHP, jQuery and the Drupal API)
  • Code colouring (keep life simple)
  • Basic syntax checking (not debugging)
  • Integrated FTP and version control client.
Tried Smultron and TextMate, which are great, but can be a bit limited. With the relaunch of Adobe's Dreamweaver (version CS5) all of the above is now possible, with a load more CMS goodies (see http://www.adobe.com/products/dreamweaver - and there's an intro on lynda.com too). OK, you only get SVN support and no Git, but everything else on my list now works well.

Here's how to set up Dreamweaver:
  1. Find the Dreamweaver configuration files, MMDocumentTypes.xml (on OSX at /Applications/Adobe Dreamweaver CS5/Configuration/DocumentTypes) and Extensions.txt (on OSX at /Applications/Adobe Dreamweaver CS5/Configuration).
  2. In MMDocumentTypes.xml add file extensions to the PHP section (you'll need to scroll through the XML to find it):

    <documenttype file="Default.php" id="PHP_MySQL" internaltype="Dynamic" macfileextension="php,php3,php4,php5,theme,module,engine,inc" servermodel="PHP MySQL" winfileextension="php,php3,php4,php5,theme,module,engine,inc" writebyteordermark="false">
    </documenttype>


  3. In Extensions.txt change the line ending in : All Documents to include MODULE,THEME,ENGINE,INFO,INC,INSTALL and the line ending in : PHP Files and append the following extensions MODULE,INC,THEME,INSTALL
  4. Open Dreamweaver and edit your preferences:
    • Code format: indent with 2 spaces; Tab size to 2
    • File Types/Editors - open .module, .theme, .engine, .info, .inc, .install in code view (add to the list of existing extensions)
  5. Create your site locally and set up in Dreamweaver (Site > Manage sites). Then open any page and it should recognize it's part of a bigger thing.... so go to Site > Site-Specific Code Hints and set the 'Structure' to Drupal. Use the same process to add specific libraries (see following video)
Err... that's it

    Tuesday, May 25, 2010

    Theming Drupal [Node] Links

    I spent ages trawling the internet trying to find easy ways to change the little links that Drupal attaches to each node ('Add new comment') and either remove them, change them to icons or shuffle the order.  Options available include:
    1. Write a custom helper module that implements hook_link_alter()
    2. Make the changes inside the theme's template.php preprocess_node
    3. Overwrite the li a attribute with CSS
    Option #1 just didn't work for me - it kept outputting the updated links in both the links ($links) and the categories ($terms).Option #3 was just bad. So I settled on #2:

    I wanted to create custom icons (rather than text) and remove a few links generated by the Feed API module; I also was using the Add this module and wanted that to be the last link at the bottom of the page. So created some nice icons (I used the wonderful FamFamFam Silk icons and a few custom drawn ones) and added them to the /images directory of my theme.  I then edited the template.php file in my theme and added the following function:

    function mytheme_preprocess_node(&$variables) {
        $patheme = drupal_get_path('theme','mytheme');
        $links = $variables['node']->links;
        global $user;
       
        //Repeat following for each $links item you wish to change
        if($links['comment_add']){
          $links['comment_add']['title'] = theme('image', $patheme.'/images/comments_add.png', t('Add a comment'),t('Add a comment'));
          $links['comment_add']['html'] = true;
        }
       
        //Remove link example: removes link to 'Original Feed' (Feed API)
        unset($links['feedapi_feed']);
       
        //Removes 'Login to post comments' link for anon users
        if (!$user->uid) {
          unset($links['comment_forbidden']);
        }
       
        //Simple way to affect order that links are rendered
        //Move addthis to end of $links
        if( isset($links['addthis']) ) {
          $addthis = $links['addthis'];
          unset($links['addthis']);
          $links = array_merge($links, array('addthis' => $addthis));
        }

        //Reset node template links
        $variables['links'] = theme_links($links, array('class' => 'links'));
    }



    And you should obviously customize accordingly. There's a little more explanation in 'Front End Drupal' (Hogbin/Kafer - Prentice Hall 2009) pp163-165 which I highly recommend. I was also using the Print module and there is advice on customizing the module's icons at http://drupal.org/node/190173#themeicon.

    And this is the end result (Left to right: Add new comment, print-friendly view and 'Add This' button). 

    Tuesday, April 06, 2010

    Creating a Custom 404 (Page Not Found) Page in Drupal

    In the event of an HTTP 404 error ("Page not found") this PHP snippet will render a site search form, but will take keywords from the user's erroneous URL and prepopulate.

    Create a new node (node/add/page), and ensure you can enter PHP code in your page body (check input filters).

    Insert the following code into the body of your new page:


    <p>I'm sorry the page you are looking for cannot be found.</p>
    <div id="error-search-box">
    <?php
    // check that the search module exists and the user has permission to hit the form
    if (module_exists('search') && user_access('search content')) {
    $path = $_REQUEST['destination'];
    $keys = strtolower(preg_replace('/[^a-zA-Z0-9-]+/', ' ', $path));
    print drupal_get_form('search_form', NULL, $keys, 'node', 'Search this site');
    }
    ?>
    </div>


    Save this new page (ideally with a custom URL of your choice: such as /page-not-found) and tweak your CSS accordingly.

    So that Drupal will send users there when HTTP 404 errors are generated, go to admin > settings > error reporting (/admin/settings/error-reporting), and set the default 404 error page to page-not-found (or whatever). (You might want to do a 403 error page while you're here too).

    If the above phases you out, as with all problems Drupal, there's a module for that... the thrifty404 module will do exactly the above, but you'll need to do a bit of cunningness in the Drupal theming layer to customize it.

    And Bob's your aunty's live-in lover..!

    Monday, November 02, 2009

    Drupal/Drush on Windows

    Spent the past 3 years getting very comfortable using a Mac/Unix environment, but have mainly been using a Windows Server at work for hosting my sites. Now striving to reach Drupal ninja status, thought it was time to use Drush on the Windows [2003] server instead of just my DEV machines.

    Pretty easy to get the basics of Drush running, although kept getting these annoying 'Cannot find module (IP-MIB): At line 0 in (none)' errors in every command being executed on the MS-DOS CLI... Bit of Googling found that this is a PHP error because the MIB files need to be on every Windows drive that you are executing PHP scripts. Even though my main website directories are on the D: drive, Drush is on my C: drive.

    So, solution is to find the \USR folder, then copy \USR\mibs to the root of each drive you want to execute Drush (or, indeed, any PHP scripts) from. If you know tons about PHP (and I don't) you can probably disable the offending PHP extensions in php.ini - but in my case 'it ain't broke' so I'm going to leave as much of my PHP configuration alone as possible.

    Thursday, May 21, 2009

    Mac/Parallels Kernel Issues after upgrade to OSX10.5.7

    I don't know if anyone else got this problem, but I recently upgraded Parallels then a few days later installed the latest OSX update to 10.5.7. After that whenever Parallels was opened received the error:


    Are you sure you want to start the virtual machine?
    One or more kernel components loaded on your Mac are for another version of Parallels Desktop. If you start the virtual machine now, it may work incorrectly. To solve this problem, restart your Mac. If the problem persists, reinstall Parallels Desktop.


    Although I could proceed, Shared Networking stopped working, and Windows/Vista crashed a few times. Searching around, found a sweet application to clean the duplicate kernel entries - published by Parallels. Worked fine for me - although note it requires a Mac OS reboot. I did not need to re-install parallels

    http://download.parallels.com/desktop/v4/en_us/parallels/update2/Cleanup-Kernel-Extensions.app.zip

    Wednesday, April 29, 2009

    GoDaddy Hosting and FTP with Dreamweaver

    Just a quick online snippet as this took me ages to sort out.
    If you have Dreamweaver and wish to use FTP to transfer your files, use the following settings in Preferences > Sites > Manage Sites > Edit > Remote Info
    • Access: FTP
    • FTP Host: yourdomain.com (no ftp:// or www prefix please)
    • Host directory: subdomain/ (ie, the folder you have your subdomain within)
    • Login: GoDaddy Primary FTP user name (or other user name if you set that up)
    • Password: doh!
    And on a Mac, I selected Passive FTP (on advice of this article)

    And, as they say in Paris: 'C'est la blaireau!'

    THAT'S THE BADGER!!!