xpath examples & domxpath use with drupal

When the wonderful Feeds module can not be used it is useful to know how to use domxpath and to write queries.
usage:

  1. $dom = new DOMDocument();
  2. @$dom->loadHTML($html);
  3. $xpath = new DOMXPath($dom);

to get the text inside of a particular tag

  1. $result = $xpath->query("//div[@id='my_id']");
  2. $text = $result->item(0)->nodeValue
  3. // or easier $xpath->query("//div[@id='my_id']/text()");

to get id's of items with a specific class

  1. $results = $xpath->query("//div[contains(@class,'my_class')]");
  2. foreach($results as $result) {
  3. $id= $result->getAttribute('id');
  4. }

to extract whole DOM html

  1. $result= $this->xpath("//body");
  2. $body = $dom->saveHTML($result->item(0));
  3. //or easier $result= $this->xpath("//body/node()");

FOR UNIT TESTING
to test if link inside of a specific tag exists

  1. $this->xpath("//div[@id='my_id']//a[contains(@href, 'my_link')]");

to test a div containing a string

  1. // exact text
  2. $this->xpath("//div[contains(@class, 'views-field-field-vintage')]/div[.='2010']");
  3. // containing text
  4. $this->xpath("//div[contains(@class, 'views-field-field-vintage')]/span[contains(., '2010')]");

to test the nth item

  1. //this checks that inside the 3rd image field is an image of 70 width with style my_style_preset
  2. $this->xpath("(//td[contains(@class, 'views-field-field-singleimage')])[3]/img[@width=70 and contains(@src, '/styles/my_style_preset/')]");
  3. // do not forget the parenthesis

some complex stuff

  1. "//td[contains(@class, 'xl7717818') or contains(@class, 'xl6717818') or contains(@class, 'xl7817818') or contains(@class, 'xl7917818')][text()!='A']/../following-sibling::tr[contains(@height, 20)]"
  2. // td OR condition and td does not have "A" text then 1 level up in the dom (to the tr tag) then select the next tr that has an height attribute of 20

traversing + position

  1. preceding-sibling::tr[1]/td[position()>2 and position()<5]
  2. // move to the previous tr then get all the td in position 3 and 4

tag that contains some text

  1. //td[contains(., 'Division')]

override a default .tpl.php file in a custom module

to basically use a template file in a custom module use the hook_theme_registry_alter(&$theme_registry) like this

Testing date fields: strtotime tips

Unit testing the creation of a node with a date field could fail because of wrong interpretation of the date by strtotime function

Enable block

There is no available API to programmatically set a block. Like Drupal core does, we have to use db directly.

simpletest: count checkboxes checked

Sometimes it is useful to know exactly the amount of options enabled with a xpath expression

Add a conditional stylesheet for IE

the options of drupal_add_css allow the use of conditional stylesheets

debug drush (and php-cli)

Drush uses the cli version of php so traditional debugging tools and functions don't work.

Create an image style

to create in code, programmatically an image style (previously called image cache preset)

Current path alias

to get the current page alias path use

Unset core stylesheet in a custom theme

by using use hook_css_alter() in template.php