Skip to content

Instantly share code, notes, and snippets.

View johnennewdeeson's full-sized avatar

John Ennew johnennewdeeson

View GitHub Profile
@johnennewdeeson
johnennewdeeson / gist:5f3f382708f3f0ca8d6e
Last active August 29, 2015 14:06
Drupal delete a field instance from an entity bundle in an update hook
/**
* Delete a field instance.
*/
function mymodule_update_7001() {
$instance = field_info_instance('entity_type', 'field_name', 'bundle');
if (!empty($instance)) {
field_delete_instance($instance, FALSE);
}
}
@johnennewdeeson
johnennewdeeson / gist:a2446f9e9bdd6f93bf80
Created September 18, 2014 09:13
Drupal disable a Views view programatically in an update hook
/**
* Disable the media browser view.
*/
function mymodule_update_7001() {
$view = views_get_view('media_default'); // media_default is the machine name of the view.
if (!empty($view)) {
ctools_include('export');
ctools_export_crud_set_status('views_view', $view, TRUE);
}
}
@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 / 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 / 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 / mymodule.install
Last active September 2, 2015 10:29
Enabling modules in an update hook.
/**
* Enable modules.
* Normally we'd use master module for this but sometimes you can't run anything other than updb in production.
*/
function mymodule_update_7001(&$sandbox) {
$modules = array(
'mymodule_one',
'mymodule_two',
'mymodule_three',
@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,
@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 / 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 / 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);