Skip to content

Instantly share code, notes, and snippets.

@yanniboi
Last active December 30, 2015 00:09
Show Gist options
  • Save yanniboi/7747268 to your computer and use it in GitHub Desktop.
Save yanniboi/7747268 to your computer and use it in GitHub Desktop.
Code snippet for field creating update hook
<?php
/**
* Code snippet for field create update hook.
*/
function module_update_7001() {
$t = get_t();
$fields = array();
if (!field_info_field('field_name')) {
$fields['field_name'] = array(
'field_name' => 'field_name',
'type' => 'image',
'cardinality' => 1,
);
}
foreach ($fields as $field) {
field_create_field($field);
}
$instances = array();
if (!field_info_field('entity', 'field_name', 'bundle')) {
$instances['field_name'] = array(
'entity_type' => 'entity',
'bundle' => 'bundle',
'field_name' => 'field_name',
'label' => $t('Field Name'),
'description' => $t('Field description'),
'settings' => array(
'file_directory' => 'images',
),
'widget' => array(
'type' => 'media_generic',
),
);
}
foreach ($instances as $instance) {
field_create_instance($instance);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment