linked_field.install in Linked Field 7
Same filename and directory in other branches
Contains install and update functions for Linked Field.
File
linked_field.installView source
<?php
/**
* @file
* Contains install and update functions for Linked Field.
*/
/**
* Implements hook_enable().
*/
function linked_field_enable() {
// We have to change the weight so we run at last.
db_update('system')
->fields(array(
'weight' => '100',
))
->condition('name', 'linked_field')
->execute();
}
/**
* Implements hook_uninstall().
*
* We have to remove Linked Field settings from all fields and views.
*/
function linked_field_uninstall() {
// Getting all fields.
$field_info = field_info_fields();
$fields = array();
foreach ($field_info as $field) {
foreach ($field['bundles'] as $entity_type => $bundles) {
foreach ($bundles as $bundle) {
// Getting field instance and modify the settings.
$instance = db_select('field_config_instance', 'fci')
->fields('fci', array(
'data',
))
->condition('field_name', $field['field_name'])
->condition('entity_type', $entity_type)
->condition('bundle', $bundle)
->execute()
->fetchField();
$instance = unserialize($instance);
foreach ($instance['display'] as &$display) {
unset($display['settings']['linked_field']);
}
$instance = serialize($instance);
// Finally we update the current field instance.
db_update('field_config_instance')
->fields(array(
'data' => $instance,
))
->condition('field_name', $field['field_name'])
->condition('entity_type', $entity_type)
->condition('bundle', $bundle)
->execute();
}
}
}
// Clear caches.
field_cache_clear();
if (db_table_exists('views_display')) {
// Getting all view displays from database.
$view_displays = db_select('views_display', 'vd')
->fields('vd')
->execute();
// Iterate all view displays and modify them.
foreach ($view_displays as $view_display) {
$vid = $view_display->vid;
$id = $view_display->id;
$display_options = unserialize($view_display->display_options);
if (isset($display_options['fields'])) {
foreach ($display_options['fields'] as &$field) {
$settings =& $field['settings'];
if (isset($settings['linked_field'])) {
unset($settings['linked_field']);
}
}
}
$display_options = serialize($display_options);
// Finally we update the current view display.
db_update('views_display')
->fields(array(
'display_options' => $display_options,
))
->condition('vid', $vid)
->condition('id', $id)
->execute();
}
}
}
/**
* Update module weight to run at last.
*/
function linked_field_update_7001(&$sandbox) {
linked_field_enable();
}
/**
* Update fields which contain Linked Field settings.
*/
function linked_field_update_7002(&$sandbox) {
// Getting all fields.
$field_info = field_info_fields();
$fields = array();
// We iterate all fields and collect them
// with their entity type and and all bundles.
foreach ($field_info as $field) {
foreach ($field['bundles'] as $entity_type => $bundles) {
foreach ($bundles as $bundle) {
$fields[] = array(
'field_name' => $field['field_name'],
'entity_type' => $entity_type,
'bundle' => $bundle,
);
}
}
}
// We initialize the batch process.
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['current_field'] = 0;
$sandbox['max'] = count($fields);
}
// Getting current setting.
$field = $fields[$sandbox['current_field']];
// Getting field instance and modify the settings.
$instance = db_select('field_config_instance', 'fci')
->fields('fci', array(
'data',
))
->condition('field_name', $field['field_name'])
->condition('entity_type', $field['entity_type'])
->condition('bundle', $field['bundle'])
->execute()
->fetchField();
$instance = unserialize($instance);
foreach ($instance['display'] as &$display) {
$settings =& $display['settings'];
// Modify settings if 'advanced' array doesn't exist.
if (isset($settings['linked_field']) && !isset($settings['linked_field']['advanced'])) {
foreach ($settings['linked_field'] as $name => $value) {
if ($name != 'linked' && $name != 'destination' && $name != 'advanced') {
// We move the setting into the 'advanced' array.
$settings['linked_field']['advanced'][$name] = $value;
unset($settings['linked_field'][$name]);
}
}
}
}
$instance = serialize($instance);
// Finally we update the current field instance.
db_update('field_config_instance')
->fields(array(
'data' => $instance,
))
->condition('field_name', $field['field_name'])
->condition('entity_type', $field['entity_type'])
->condition('bundle', $field['bundle'])
->execute();
// Prepeare the values for the next step.
$sandbox['progress']++;
$sandbox['current_field']++;
// Update process is finished when all settings have been ran.
$sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
// Clear caches if all fields are processed.
if (intval($sandbox['#finished'])) {
field_cache_clear();
}
}
/**
* Update views which contain Linked Field settings.
*/
function linked_field_update_7003(&$sandbox) {
if (db_table_exists('views_display')) {
// Getting all view displays from database.
$view_displays = db_select('views_display', 'vd')
->fields('vd')
->execute();
$view_displays_count = db_select('views_display', 'vd')
->countQuery()
->execute()
->fetchField();
// We initialize the batch process.
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['current_view_display'] = 1;
$sandbox['max'] = $view_displays_count;
}
// Iterate all view displays and modify them.
foreach ($view_displays as $view_display) {
$vid = $view_display->vid;
$id = $view_display->id;
$display_options = unserialize($view_display->display_options);
if (isset($display_options['fields'])) {
foreach ($display_options['fields'] as &$field) {
$settings =& $field['settings'];
if (isset($settings['linked_field'])) {
foreach ($settings['linked_field'] as $name => $value) {
if ($name != 'linked' && $name != 'destination' && $name != 'advanced') {
// We move the setting into the 'advanced' array.
$settings['linked_field']['advanced'][$name] = $value;
unset($settings['linked_field'][$name]);
}
}
}
}
}
$display_options = serialize($display_options);
// Finally we update the current view display.
db_update('views_display')
->fields(array(
'display_options' => $display_options,
))
->condition('vid', $vid)
->condition('id', $id)
->execute();
// Prepeare the values for the next step.
$sandbox['progress']++;
$sandbox['current_view_display']++;
// Update process is finished when all settings have been ran.
$sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
}
}
}
/**
* Add some settings to fields if they don't exist already.
*/
function linked_field_update_7004(&$sandbox) {
// Getting all fields.
$field_info = field_info_fields();
$fields = array();
// We iterate all fields and collect them
// with their entity type and and all bundles.
foreach ($field_info as $field) {
foreach ($field['bundles'] as $entity_type => $bundles) {
foreach ($bundles as $bundle) {
$fields[] = array(
'field_name' => $field['field_name'],
'entity_type' => $entity_type,
'bundle' => $bundle,
);
}
}
}
// We initialize the batch process.
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['current_field'] = 0;
$sandbox['max'] = count($fields);
}
// Getting current setting.
$field = $fields[$sandbox['current_field']];
// Getting field instance and modify the settings.
$instance = db_select('field_config_instance', 'fci')
->fields('fci', array(
'data',
))
->condition('field_name', $field['field_name'])
->condition('entity_type', $field['entity_type'])
->condition('bundle', $field['bundle'])
->execute()
->fetchField();
$instance = unserialize($instance);
foreach ($instance['display'] as &$display) {
$settings =& $display['settings'];
// Iterate all settings from 'advanced' array.
if (isset($settings['linked_field'])) {
foreach (array(
'title',
'target',
'class',
'rel',
'text',
) as $setting) {
if (!isset($settings['linked_field']['advanced'][$setting])) {
$settings['linked_field']['advanced'][$setting] = '';
}
}
}
}
$instance = serialize($instance);
// Finally we update the current field instance.
db_update('field_config_instance')
->fields(array(
'data' => $instance,
))
->condition('field_name', $field['field_name'])
->condition('entity_type', $field['entity_type'])
->condition('bundle', $field['bundle'])
->execute();
// Prepeare the values for the next step.
$sandbox['progress']++;
$sandbox['current_field']++;
// Update process is finished when all settings have been ran.
$sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
// Clear caches if all fields are processed.
if (intval($sandbox['#finished'])) {
field_cache_clear();
}
}
/**
* Add some settings to views fields if they don't exist already.
*/
function linked_field_update_7005(&$sandbox) {
if (db_table_exists('views_display')) {
// Getting all view displays from database.
$view_displays = db_select('views_display', 'vd')
->fields('vd')
->execute();
$view_displays_count = db_select('views_display', 'vd')
->countQuery()
->execute()
->fetchField();
// We initialize the batch process.
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['current_view_display'] = 1;
$sandbox['max'] = $view_displays_count;
}
// Iterate all view displays and modify them.
foreach ($view_displays as $view_display) {
$vid = $view_display->vid;
$id = $view_display->id;
$display_options = unserialize($view_display->display_options);
if (isset($display_options['fields'])) {
foreach ($display_options['fields'] as &$field) {
$settings =& $field['settings'];
if (isset($settings['linked_field'])) {
// Remove 'foo' which was introduced with update 7003 accidentally.
unset($settings['linked_field']['foo']);
// Iterate all settings from 'advanced' array.
foreach (array(
'title',
'target',
'class',
'rel',
'text',
) as $setting) {
if (!isset($settings['linked_field']['advanced'][$setting])) {
$settings['linked_field']['advanced'][$setting] = '';
}
}
}
}
}
$display_options = serialize($display_options);
// Finally we update the current view display.
db_update('views_display')
->fields(array(
'display_options' => $display_options,
))
->condition('vid', $vid)
->condition('id', $id)
->execute();
// Prepeare the values for the next step.
$sandbox['progress']++;
$sandbox['current_view_display']++;
// Update process is finished when all settings have been ran.
$sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
}
}
}
/**
* Update module weight to run at last.
*/
function linked_field_update_7006(&$sandbox) {
linked_field_enable();
}
Functions
Name | Description |
---|---|
linked_field_enable | Implements hook_enable(). |
linked_field_uninstall | Implements hook_uninstall(). |
linked_field_update_7001 | Update module weight to run at last. |
linked_field_update_7002 | Update fields which contain Linked Field settings. |
linked_field_update_7003 | Update views which contain Linked Field settings. |
linked_field_update_7004 | Add some settings to fields if they don't exist already. |
linked_field_update_7005 | Add some settings to views fields if they don't exist already. |
linked_field_update_7006 | Update module weight to run at last. |