Skip to content

Instantly share code, notes, and snippets.

View johnennewdeeson's full-sized avatar

John Ennew johnennewdeeson

View GitHub Profile
@johnennewdeeson
johnennewdeeson / drupal-bootstrap-css-masquerade-navbar
Created October 25, 2016 15:53
CSS for the masquerade module when inserted into the navbar region
/**
* Masquerade block in the Navbar region.
*/
#masquerade-block-1--2 ul {
width: 300px !important;
border: 1px solid grey;
}
#masquerade-block-1--2 li.selected {
background-color: #eee;
/**
* Masquerade block in the Navbar Region.
*/
#masquerade-block-1 div#autocomplete ul {
padding-top: 3px;
float: left;
width: 300px !important;
margin-top: 30px;
position: absolute;
@johnennewdeeson
johnennewdeeson / play.drush.inc
Created April 12, 2016 08:31
Executing batched operation with Drush
<?php
/**
* @file
* sites/all/drush/play.drush.php
*
* Oftentimes one needs to run an operation on a list of things.
* If this list is of variable length, running it in one process may
* cause a time out and/or out of memory.
*
@johnennewdeeson
johnennewdeeson / FeaturesContext.php
Created January 17, 2016 21:22
CKEditor fill in field in a Behat step definition
/**
* @Then I fill in wysiwyg on field :locator with :value
*/
public function iFillInWysiwygOnFieldWith($locator, $value) {
$el = $this->getSession()->getPage()->findField($locator);
if (empty($el)) {
throw new ExpectationException('Could not find WYSIWYG with locator: ' . $locator, $this->getSession());
}
@johnennewdeeson
johnennewdeeson / drupal_run_as.php
Last active October 25, 2015 07:28 — forked from angry-dan/drupal_run_as.php
Safely run a function in Drupal as another user.
<?php
function drupal_run_as($user, $func) {
global $user;
$original_user = $user;
$old_state = drupal_save_session();
drupal_save_session(FALSE);
$args = func_get_args();
$user = array_shift($args);
@johnennewdeeson
johnennewdeeson / FeatureContext_fill_in_drupal_autocomplete.inc
Created October 12, 2015 12:49
Use behat extension to fill in a Drupal autocomplete field and select the right option which appears.
/**
* @When I fill in the autocomplete :autocomplete with :text and click :popup
*/
public function fillInDrupalAutocomplete($autocomplete, $text, $popup) {
$el = $this->getSession()->getPage()->findField($autocomplete);
$el->focus();
// Set the autocomplete text then put a space at the end which triggers
// the JS to go do the autocomplete stuff.
$el->setValue($text);
@johnennewdeeson
johnennewdeeson / make_user_go_to_edit
Last active September 20, 2015 13:17
Drupal 7 make /user go to the user edit form when logged in for sites that don't have user profile pages and you don't want a full page view of a user.
/**
* Implements hook_menu_alter().
*/
function mymodule_menu_alter(&$items) {
if (isset($items['user/%user'])) {
// Turn user/%user into an edit form callback rather than view.
$items['user/%user']['page callback'] = 'drupal_get_form';
$items['user/%user']['page arguments'] = array('user_profile_form', 1);
$items['user/%user']['access callback'] = 'user_edit_access';
$items['user/%user']['access arguments'] = array(1);
@johnennewdeeson
johnennewdeeson / view_query_alter_example.php
Last active September 18, 2015 11:45
Drupal views hook_views_query_alter move a where clause restriction to a join
/**
* Implements hook_views_query_alter().
*
* This is a view of users and the view has a relationship to node and a contextual filter.
* There is an entity reference field on the user pointing to the node called field_node_entity_reference.
*
* By placing the restiction on the join and not requiring the relationship means we can additional exposed filters
* such as where node.nid is empty (users who do not link to the node).
*/
function mymodule_views_query_alter(&$view, &$query) {
@johnennewdeeson
johnennewdeeson / PersonaContext.php
Last active September 20, 2015 13:45
A way of organising personas (user types) in your behat tests
<?php
/**
* @file
*
* Add this code in a file features/bootstrap/PersonaContext.php
*
* Include the PersonaContext in behat.yml like this ...
*
* default:
@johnennewdeeson
johnennewdeeson / BehatClickLinkInTableRow
Last active September 17, 2015 11:40
Drupal Behat Step definition for clicking an operations link in a table where another cell in the same row contains a known value
/**
* @When I click :link_text in table row containing :text
*/
public function iClickOperationLinkInTableRowWithText($link_text, $text) {
$session = $this->getSession();
$el = $session->getPage()->find('xpath', "//td[contains(., '{$text}')]");
if (empty($el)) {
throw new ExpectationException(t('No such text in table - @text', array(
'@text' => $text,