Skip to content

Instantly share code, notes, and snippets.

@angry-dan
Last active August 29, 2015 14:02
Show Gist options
  • Save angry-dan/ae7a7da430288607685f to your computer and use it in GitHub Desktop.
Save angry-dan/ae7a7da430288607685f to your computer and use it in GitHub Desktop.
<?php
/**
* Helper for creating new beans.
*
* @see https://gist.github.com/dan-sprog/ae7a7da430288607685f
*/
function my_module_blocks_install_bean($info, $overwrite = FALSE, $needs_revert = FALSE) {
if ($overwrite) {
$bean = bean_load_delta($info['delta']);
if ($bean) {
$bean->delete();
$needs_revert = TRUE;
}
}
$bean = bean_create($info);
try {
if ($bean->save()) {
if ($needs_revert) {
features_revert_module('my_module_blocks');
}
return t('Created new bean @label', array('@label' => $info['label']));
}
}
// Unless we got an explicit positive result from $bean->save() we assume
// failure anyway.
catch (PDOException $ex) {}
// Handle failed saves and db exceptions.
throw new DrupalUpdateException(format_string('Failed to create bean @label with machine name @machine_name. Maybe it already exists?', array(
'@label' => $info['label'],
'@machine_name' => $info['delta'],
)));
}
// Example usage:
function my_module_update_7001() {
my_module_blocks_install_bean(array(
'delta' => 'bean-delta-max-32-chars',
'type' => 'bean-bundle',
'label' => 'admin-only-label',
'title' => 'Bean Title or <none>',
'field_block_text' => array(
LANGUAGE_NONE => array(
array(
'format' => 'full_html',
'value' => 'Sensible default text for this bean',
),
),
)
));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment