Skip to content

Instantly share code, notes, and snippets.

View johnennewdeeson's full-sized avatar

John Ennew johnennewdeeson

View GitHub Profile
@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 / 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 / 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 / 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: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);
}
}