Posts tagged with "Drupal"

Posted: Tuesday, January 28th 2014

There are some good examples out there on how to create a custom Entity reference Behaviour plugin, but not so much for Selection handlers.

Why would you want to do this? A number of reasons. For me it was because the base Entity reference selection handler wasn’t returning correct results on an autocomplete field and I've got custom access rules to consider. An alternative to making our own would be to use Views, but this idea doesn’t appeal due to the performance overhead compared to EntityField Query.

Posted: Monday, January 27th 2014
Tags:

Too long! Incoming wave of posts about things I've learned creating Harmony Forum, mostly around Entity API and Entity reference…

Posted: Monday, February 13th 2012

If you ever used the Pathauto module and the very useful [menupath-raw] token in Drupal 7 this is gone.

Using the latest stable release of Token module, this is now the way!

[node:menu-link:parents:join:/]/[node:title]

 

The use case for this would be that you have a page at /shop, and you want to create a sub-page under it (let's say we're creating a Help page), and have that hierarchy reflected in the URL, you'd set the parent menu item for your new node as "Shop", then you'd get the URL:

/shop/help

If you just had the node at the top level of a menu, not nested in any way you'd get:

/help

Important! To prevent pathauto stripping out slashes found within tokens you must go to Pathauto's settings, under punctuation for "Slash" (/) set this to "Do nothing". Don't worry about double slashes as Pathauto will take care of that for you.

Posted: Sunday, August 21st 2011

Here's a couple tips for views theming, great fun! Also an opportunity for the longest post title in the world.

First, How to get the row id from within the fields template.

When doing your own custom views theming, it can be useful to know which row number you're currently working with. A good example of this usage would be super-sizing certain elements for the first row of a listing.

To begin let's take a look at the default fields template views-view-fields.tpl.php.

<?php
/**
* @file views-view-fields.tpl.php
* Default simple view template to all the fields as a row.
*
* - $view: The view in use.
* - $fields: an array of $field objects. Each one contains:
*   - $field->content: The output of the field.
*   - $field->raw: The raw data for the field, if it exists. This is NOT output safe.
*   - $field->class: The safe class id to use.
*   - $field->handler: The Views field handler object controlling this field. Do not use
*     var_export to dump this object, as it can't handle the recursion.
*   - $field->inline: Whether or not the field should be inline.
*   - $field->inline_html: either div or span based on the above flag.
*   - $field->wrapper_prefix: A complete wrapper containing the inline_html to use.
*   - $field->wrapper_suffix: The closing tag for the wrapper.
*   - $field->separator: an optional separator that may appear before a field.
*   - $field->label: The wrap label text to use.
*   - $field->label_html: The full HTML of the label to use including
*     configured element type.
* - $row: The raw result object from the query, with all data it fetched.
*
* @ingroup views_templates
*/
?>

<?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
  <?php endif; ?>

  <?php print $field->wrapper_prefix; ?>
    <?php print $field->label_html; ?>
    <?php print $field->content; ?>
  <?php print $field->wrapper_suffix; ?>
<?php endforeach; ?>

At the top of the file you've got a listing of the variables available to you, very useful! But there is an extra variable not listed: $id.

At a guess, it's not mentioned as the variable name is re-used in the foreach loop, before this point you can re-assign it or re-use it.

Depending on the nature of theming we are carrying out, sometimes it's necessary to have a specific HTML structure to each row, here's an example of this, making use of $id to add an extra class to the first item.

<?php
// $Id$
/**
* @file views-view-fields.tpl.php
*/

$wrapper_classes = 'wrapper';

if (
$id == 0) {
   
$wrapper_classes .= ' supersize';
}
?>

<div class="<?php print $wrapper_classes; ?>">
  <div class="left image">
  <?php print $fields['field_image_fid']->content; ?>
  </div>
  <div class="left text">
  <h2><?php print $fields['title']->content; ?></h2>
  <?php print $fields['teaser']->content; ?>
  </div>
</div>

Second, How to tell how many total rows a view has found, not just the number it's currently displaying.

This one's actually really easy, where-ever you have a view, in a view template, when running a view in code, you can do:

<?php
$view
= views_get_view('view_machine_name');
$view->execute();

$view->total_rows;
?>

One caveat, for this to work, your view needs to be using a pager. If you're not there are other ways, but it's a bit more involved.

Posted: Saturday, August 6th 2011

If you've tweaked your user register form, and now for some reason you can't add a new user via the admin section. It's entirely possble that you've changed the submit buttons value, like:

/**
* Implementation of hook_form_alter().
*/
function mymodule_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'user_register') {
    $form['submit']['#value'] = t('Register');
  }
}

The user_register form is re-used in the admin section, so any tweaks you make will appear here also. Unusually if you change the submit value, then the admin user register form will break, and bounce you back to admin/user/user without an error message.

All you need to do is make the submit value tweak specific to only the front-end form:

/**
* Implementation of hook_form_alter().
*/
function mymodule_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'user_register') {
    if (arg(0) == 'user') {
      $form['submit']['#value'] = t('Register');
    }
  }
}

Et voilà!

Posted: Wednesday, June 29th 2011

I'm thinking there should be a Drupal on the edge series, featuring all the weird and wonderful problems you're never likely to encounter :)

Here's one of them, recently I've had to work with negative timestamps and Drupal, which as it happens doesn't support negative timestamps.

The issue is one more of PHP compatibility than Drupal, providing you're running PHP >= 5.1 all you need to do is apply the patch from the following d.org issue:

http://drupal.org/node/271398#comment-888391
http://drupal.org/files/issues/node.module_80.patch

 

That's it! But to help you with whatever troubles you may have, I came across this very helpful post on how to convert dates to timestamps whilst keeping in mind negative timestamps, and vice versa:

http://www.epochconverter.com/programming/mysql-from-unixtime.php (See the heading "Negative epochs" on this page).

Posted: Thursday, June 16th 2011

A quick gotcha explanation if you find that Drupal's block cache isn't working.

Chances are this could be described somewhere, but after a bit of Googling and it not turning up, I figure put it up!

Pretty simple really, Drupal won't cache blocks for User 1 (block.module, line 28):

/*
* Note that user 1 is excluded from block caching.
*/

 

There you have it!