Skip to content

Instantly share code, notes, and snippets.

View johnennewdeeson's full-sized avatar

John Ennew johnennewdeeson

View GitHub Profile
@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 / mymodule.install
Created September 2, 2015 10:21
Rebuild all content node access permissions in an update hook
/**
* Rebuild node access permissions.
* node_access_rebuild(TRUE) claims to be able to do this in batch mode but all it does is set a
* batch. There is no way to process the batch from within an update hook. You could do it using
* a separate drush command but if you only have access to run updb on your production env
* then you need to do all the processing from within the update hook. This gem allows complete
* safe permission rebuild, batched, from within a single update hook.
*/
function mymodule_update_7001(&$sandbox) {
_node_access_rebuild_batch_operation($sandbox);
@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 / gist:22196a15b3ea115a2451
Last active May 23, 2019 13:32
Make alt text required when uploading images using media widget (Drupal)
/**
* Implements hook_form_FORM_ID_alter().
*/
function mymodule_form_file_entity_add_upload_alter(&$form) {
_mymodule_media_form_alter($form);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
@johnennewdeeson
johnennewdeeson / gist:26a1f0082aa3f5c4dd47
Last active January 11, 2017 23:10
Drupal move all data from one field to another on the same node programatically.
/**
* Move all data in one field to another.
* In the example below we replace a multi cardinality field (field_old_field)
* with a single cardinality field (field_new_field).
* The data in the first element of the old field becomes the only data in the new field.
*
* @requires entity module
*/
function mymodule_update_7001(&$sandbox) {
if (!isset($sandbox['progress'])) {
@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 / 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 / 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: