123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- <?php
- /**
- * @file
- * Install file providing corresponding entity reference schema.
- */
- /**
- * Implements hook_schema().
- */
- function cer_schema() {
- $schema = array();
- // @see entity_exportable_schema_fields()
- $schema['cer_preset'] = array(
- 'fields' => array(
- 'pid' => array(
- 'type' => 'serial',
- 'not null' => TRUE,
- ),
- 'identifier' => array(
- 'type' => 'text',
- 'size' => 'tiny',
- 'not null' => TRUE,
- 'description' => "The preset's machine-readable export identifier.",
- ),
- 'status' => array(
- 'type' => 'int',
- 'size' => 'tiny',
- 'not null' => TRUE,
- 'default' => 0x01, // ENTITY_CUSTOM
- 'description' => "The preset's exportable status.",
- ),
- 'module' => array(
- 'type' => 'varchar',
- 'length' => 255,
- 'not null' => FALSE,
- 'default' => 'cer',
- 'description' => 'The module, if any, which exports this preset.',
- ),
- ),
- 'primary key' => array('pid'),
- );
- return $schema;
- }
- /**
- * Implements hook_install().
- */
- function cer_install() {
- field_info_cache_clear();
- $fields = _cer_field_definitions();
- array_walk($fields, 'field_create_field');
- $instances = _cer_field_instance_definitions();
- array_walk($instances, 'field_create_instance');
- if (module_exists('corresponding_node_references')) {
- _cer_hijack_cnr();
- }
- }
- /**
- * Contains the default field definitions for presets.
- */
- function _cer_field_definitions() {
- $field_bases = array();
- $field_bases[] = array(
- 'active' => 1,
- 'cardinality' => 1,
- 'deleted' => 0,
- 'entity_types' => array(),
- 'field_name' => 'cer_bidirectional',
- 'foreign keys' => array(),
- 'indexes' => array(
- 'value' => array(
- 0 => 'value',
- ),
- ),
- 'locked' => 0,
- 'module' => 'list',
- 'settings' => array(
- 'allowed_values' => array(
- 0 => 0,
- 1 => 1,
- ),
- 'allowed_values_function' => '',
- ),
- 'translatable' => 0,
- 'type' => 'list_boolean',
- );
- $field_bases[] = array(
- 'active' => 1,
- 'cardinality' => 1,
- 'deleted' => 0,
- 'entity_types' => array(),
- 'field_name' => 'cer_enabled',
- 'foreign keys' => array(),
- 'indexes' => array(
- 'value' => array(
- 0 => 'value',
- ),
- ),
- 'locked' => 1,
- 'module' => 'list',
- 'settings' => array(
- 'allowed_values' => array(
- 0 => 0,
- 1 => 1,
- ),
- 'allowed_values_function' => '',
- ),
- 'translatable' => 0,
- 'type' => 'list_boolean',
- );
- $field_bases[] = array(
- 'active' => 1,
- 'cardinality' => 1,
- 'deleted' => 0,
- 'entity_types' => array(),
- 'field_name' => 'cer_weight',
- 'foreign keys' => array(),
- 'indexes' => array(
- 'value' => array(
- 0 => 'value',
- ),
- ),
- 'locked' => 0,
- 'module' => 'list',
- 'settings' => array(
- 'allowed_values' => array(),
- 'allowed_values_function' => 'cer_weight_options',
- ),
- 'translatable' => 0,
- 'type' => 'list_integer',
- );
- $field_bases[] = array(
- 'active' => 1,
- 'cardinality' => 1,
- 'deleted' => 0,
- 'entity_types' => array(),
- 'field_name' => 'cer_left',
- 'foreign keys' => array(
- 'field_instance' => array(
- 'columns' => array(
- 'fiid' => 'id',
- ),
- 'table' => 'field_config_instance',
- ),
- ),
- 'indexes' => array(),
- 'locked' => 1,
- 'module' => 'field_object',
- 'settings' => array(),
- 'translatable' => 0,
- 'type' => 'field_object',
- );
- $field_bases[] = array(
- 'active' => 1,
- 'cardinality' => 1,
- 'deleted' => 0,
- 'entity_types' => array(),
- 'field_name' => 'cer_right',
- 'foreign keys' => array(
- 'field_instance' => array(
- 'columns' => array(
- 'fiid' => 'id',
- ),
- 'table' => 'field_config_instance',
- ),
- ),
- 'indexes' => array(),
- 'locked' => 1,
- 'module' => 'field_object',
- 'settings' => array(),
- 'translatable' => 0,
- 'type' => 'field_object',
- );
- return $field_bases;
- }
- /**
- * Contains the default field instance definitions for presets.
- */
- function _cer_field_instance_definitions() {
- $field_instances = array();
- $field_instances[] = array(
- 'bundle' => 'cer',
- 'default_value' => array(
- 0 => array(
- 'value' => 1,
- ),
- ),
- 'deleted' => 0,
- 'description' => '',
- 'display' => array(
- 'default' => array(
- 'label' => 'above',
- 'module' => 'list',
- 'settings' => array(),
- 'type' => 'list_default',
- 'weight' => 1,
- ),
- ),
- 'entity_type' => 'cer',
- 'field_name' => 'cer_bidirectional',
- 'label' => 'Bidirectional',
- 'required' => 0,
- 'settings' => array(),
- 'widget' => array(
- 'active' => 1,
- 'module' => 'options',
- 'settings' => array(
- 'display_label' => 1,
- ),
- 'type' => 'options_onoff',
- 'weight' => 3,
- ),
- );
- $field_instances[] = array(
- 'bundle' => 'cer',
- 'default_value' => array(
- 0 => array(
- 'value' => 1,
- ),
- ),
- 'deleted' => 0,
- 'description' => '',
- 'display' => array(
- 'default' => array(
- 'label' => 'above',
- 'module' => 'list',
- 'settings' => array(),
- 'type' => 'list_default',
- 'weight' => 0,
- ),
- ),
- 'entity_type' => 'cer',
- 'field_name' => 'cer_enabled',
- 'label' => 'Enabled',
- 'required' => 0,
- 'settings' => array(),
- 'widget' => array(
- 'active' => 1,
- 'module' => 'options',
- 'settings' => array(
- 'display_label' => 1,
- ),
- 'type' => 'options_onoff',
- 'weight' => 2,
- ),
- );
- $field_instances[] = array(
- 'bundle' => 'cer',
- 'default_value' => array(
- 0 => array(
- 'value' => 0,
- ),
- ),
- 'deleted' => 0,
- 'description' => '',
- 'display' => array(
- 'default' => array(
- 'label' => 'above',
- 'module' => 'list',
- 'settings' => array(),
- 'type' => 'list_default',
- 'weight' => 4,
- ),
- ),
- 'entity_type' => 'cer',
- 'field_name' => 'cer_weight',
- 'label' => 'Weight',
- 'required' => 1,
- 'settings' => array(),
- 'widget' => array(
- 'active' => 1,
- 'module' => 'options',
- 'settings' => array(),
- 'type' => 'options_select',
- 'weight' => 4,
- ),
- );
- $field_instances[] = array(
- 'bundle' => 'cer',
- 'default_value' => NULL,
- 'deleted' => 0,
- 'description' => '',
- 'display' => array(
- 'default' => array(
- 'label' => 'hidden',
- 'module' => NULL,
- 'settings' => array(),
- 'type' => 'field_object_default',
- 'weight' => 5,
- ),
- ),
- 'entity_type' => 'cer',
- 'field_name' => 'cer_left',
- 'label' => 'Left Field',
- 'required' => 1,
- 'settings' => array(
- 'function' => 'CerFieldChain::collectAll',
- ),
- 'widget' => array(
- 'active' => 1,
- 'module' => 'options',
- 'settings' => array(),
- 'type' => 'options_select',
- 'weight' => 0,
- ),
- );
- $field_instances[] = array(
- 'bundle' => 'cer',
- 'default_value' => NULL,
- 'deleted' => 0,
- 'description' => '',
- 'display' => array(
- 'default' => array(
- 'label' => 'hidden',
- 'module' => NULL,
- 'settings' => array(),
- 'type' => 'field_object_default',
- 'weight' => 6,
- ),
- ),
- 'entity_type' => 'cer',
- 'field_name' => 'cer_right',
- 'label' => 'Right Field',
- 'required' => 1,
- 'settings' => array(
- 'function' => 'CerFieldChain::collectAll',
- ),
- 'widget' => array(
- 'active' => 1,
- 'module' => 'options',
- 'settings' => array(),
- 'type' => 'options_select',
- 'weight' => 1,
- ),
- );
- return $field_instances;
- }
- /**
- * Implements hook_uninstall().
- */
- function cer_uninstall() {
- variable_del('cer_debug');
- // Delete fields created by cer_install()
- field_delete_field('cer_left');
- field_delete_field('cer_right');
- field_delete_field('cer_bidirectional');
- field_delete_field('cer_enabled');
- field_delete_field('cer_weight');
- }
- /**
- * Converts all CNR presets to CER presets, then disables CNR.
- */
- function _cer_hijack_cnr() {
- $cnr = db_query('SELECT * FROM {corresponding_node_references} WHERE 1')->fetchAll();
- foreach ($cnr as $index => $preset) {
- // Prepend 'node*' to each side of the preset. CNR, sensibly enough, assumes everything
- // is on a node, but CER needs this specified explicitly.
- $key = explode('*', $preset->node_types_content_fields);
- $cnr[$index]->entity_types_content_fields = "node*{$key[0]}*{$key[1]}*node*{$key[2]}*{$key[3]}";
- }
- array_walk($cnr, '_cer_rebuild_preset');
- module_disable(array('corresponding_node_references'));
- drupal_set_message(t('Rebuilt CNR presets.'));
- }
- /**
- * Rename table to shorten module name.
- */
- function cer_update_7001() {
- if (db_table_exists('corresponding_entity_references')) {
- db_rename_table('corresponding_entity_references', 'cer');
- }
- }
- /**
- * Creates the {cer_preset} table to store CerPreset entities, and rebuilds
- * legacy presets.
- */
- function cer_update_7005() {
- // Register new classes with the autoloader.
- registry_rebuild();
- // Create the table for preset entities.
- $tables = cer_schema();
- db_create_table('cer_preset', $tables['cer_preset']);
- // Install Field Reference, which powers the cer_left and cer_right fields.
- if (! module_exists('field_object')) {
- module_enable(array('field_object'));
- }
- // Create preset entities' fields.
- cer_install();
- // Convert legacy presets to entities, including 1.x, 2.x and the older
- // 3.x schemas.
- $old = db_select('cer')->fields('cer')->execute()->fetchAll();
- $old = array_merge($old, module_invoke_all('cer_default_presets'));
- array_walk($old, '_cer_rebuild_preset');
- // So long, legacy table...
- db_drop_table('cer');
- // Delete defunct status variable maintained by CTools.
- variable_del('default_cer');
- if (module_exists('corresponding_node_references')) {
- _cer_hijack_cnr();
- }
- }
- /**
- * Rebuilds a legacy CER or CNR preset as a CerPreset entity.
- */
- function _cer_rebuild_preset($old) {
- if (isset($old->entity_types_content_fields)) {
- $keys = explode('*', $old->entity_types_content_fields);
- $old->a = implode(':', array_slice($keys, 0, 3));
- $old->b = implode(':', array_slice($keys, 3));
- }
- if (! isset($old->bidirectional)) {
- $old->bidirectional = TRUE;
- }
- if (! isset($old->weight)) {
- $old->weight = 0;
- }
- // If the 'enabled' flag doesn't exist, we can presume that this preset
- // was exported in code generated by CTools, which means that it should
- // have a 'disabled' flag, which we can invert to get the status.
- if (! isset($old->enabled)) {
- $old->enabled = (! $old->disabled);
- }
- // Validate both field chains by trying to unpack them. If any field in
- // either chain isn't exposed by hook_cer_fields(), an exception will be
- // thrown and we can bail out with an error message.
- try {
- CerFieldChain::unpack($old->a);
- CerFieldChain::unpack($old->b);
- }
- catch (Exception $e) {
- $variables = array(
- '%key' => "{$old->a}*{$old->b}",
- );
- return drupal_set_message(t('Could not rebuild preset %key because it refers to invalid fields.', $variables), 'error');
- }
- $preset = new CerPreset();
- $preset->wrapper->cer_left->set($old->a);
- $preset->wrapper->cer_right->set($old->b);
- $preset->wrapper->cer_enabled->set($old->enabled);
- $preset->wrapper->cer_bidirectional->set($old->bidirectional);
- $preset->wrapper->cer_weight->set($old->weight);
- $preset->save();
- }
|