Commit 768ae884 authored by bgm's avatar bgm Committed by Aegir user

Add Drupal contrib module blockreference (req. by Nic).

parent 2330c957
This diff is collapsed.
<?php
/**
* @file
* Support for processing blockreference fields in Migrate.
*/
class MigrateBlockReferenceFieldHandler extends MigrateSimpleFieldHandler {
public function __construct($options = array()) {
parent::__construct(array(
'value_key' => 'moddelta',
'skip_empty' => TRUE,
));
$this->registerTypes(array('blockreference'));
}
}
Block reference README
CONTENTS OF THIS FILE
----------------------
* Introduction
* Installation
* Usage
* Theming
* Notes
INTRODUCTION
------------
Defines a field type Block reference which creates a relationship to a block and
allows the block to be displayed as the content of the field.
Project page: http://drupal.org/project/blockreference.
INSTALLATION
------------
Install and enable the Block reference module.
For detailed instructions on installing contributed modules see:
http://drupal.org/documentation/install/modules-themes/modules-7
USAGE
-----
Block reference fields will now be available in the Field UI.
For detailed instructions on using the Field UI see:
http://drupal.org/documentation/modules/field-ui
THEMEING
--------
To assist in themeing blocks that are displayed using the block reference
module, the block reference element is available in the $variables array in
template_preprocess_block().
The element can be found at:
$variables['elements']['#blockreference_element']
Note that the element will not be there for non-blockreference blocks so you
should first check the existence of the element before using its contents.
NOTES
-----
* Relationships are saved using the block's `bid`, not its delta +
module.
* Block configuration visibility settings are respected since version
7.x-1.14. If a referenced block does not appear when viewing the node,
check the block's visibility settings on /admin/structure/block. Note
that visibility settings are evaluated regardless of whether the
block is assigned to a region.
<?php
/**
* Alter referenceable blocks, BEFORE the autocomplete match.
*
* These are always block objects, not options. You can change the option label
* by changing $block->info.
*
* For autocomplete fields, this alter happens AFTER the search, before slicing
* the results (ACDB does that).
*
* $context contains:
* - instance
* - type ("autocomplete" or "options_list", depending on where this list is
* requested)
* - entity
* - entity_type
*/
function hook_blockreference_blocks_pre_alter(&$blocks, $context) {
// These are all the blocks that match the field instance's referenceable module option. If you were to change
// a block's label now, that's the label the autocomplete will try to match.
foreach ($blocks as $id => $block) {
// Add module & delta to pretty label.
$block->info .= ' (' . $block->module . '/' . $block->delta . ')';
// Remove all Views blocks, except 'public_stuff'.
if ($block->module == 'views' && strpos($block->delta, 'public_stuff-') !== 0) {
unset($blocks[$id]);
}
}
}
/**
* Alter referenceable blocks, AFTER the autocomplete match.
*
* Same exact API as hook_blockreference_blocks_pre_alter().
*/
function hook_blockreference_blocks_post_alter(&$blocks, $context) {
// These are the exact blocks in the result. If the widget is an autocomplete, it will be cut off after 10 (?)
// elements, so sorting is important. If you change the label here, the displayed labels will be different than
// the matched labels, so probably don't do that.
// Reverse sort, because I'm like that.
uasort($blocks, function ($a, $b) {
return strnatcasecmp($b->info, $a->info);
});
}
<?php
/**
* Implements hook_feeds_processor_targets_alter().
*
* @see FeedsNodeProcessor::getMappingTargets()
*/
function blockreference_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
$info = field_info_field($name);
if ($info['type'] == 'blockreference') {
$targets[$name . ':moddelta'] = array(
'name' => check_plain($instance['label']) . ' (moddelta)',
'callback' => 'blockreference_feeds_set_target',
'description' => t('The field instance @label of @field_name matched by moddelta.', array(
'@label' => $instance['label'],
'@field_name' => $name,
)),
'real_target' => $name,
);
}
}
}
/**
* Block reference callback for mapping.
*
* When the callback is invoked, $target contains the name of the field the
* user has decided to map to and $value contains the value of the feed item
* element the user has picked as a source.
*
* @param $source
* A FeedsSource object.
* @param $entity
* The entity to map to.
* @param $target
* The target key on $entity to map to.
* @param $value
* The value to map. MUST be an array.
*/
function blockreference_feeds_set_target($source, $entity, $target, $value) {
// Assume that the passed in value could really be any number of values.
$values = array_filter((array) $value);
// Don't do anything if we weren't given any data.
if (!$value) {
return;
}
list($target, $match_key) = explode(':', $target, 2);
// Set the language of the field depending on the mapping.
$language = isset($match_key['language']) ? $match_key['language'] : LANGUAGE_NONE;
// Iterate over all values.
$items = array();
foreach ($values as $value) {
@list($module, $delta) = explode(':', $value);
if ($module && $delta) {
$exists = db_query('SELECT bid FROM {block} WHERE module = ? AND delta = ?', array(
$module,
$delta,
))->fetchField();
if ($exists) {
$items[$language][]['moddelta'] = $value;
}
}
}
$entity->$target = $items;
}
This diff is collapsed.
name = Block reference
description = Defines a field type for referencing a block from a node.
package = Fields
core = 7.x
dependencies[] = field
dependencies[] = options
dependencies[] = block
dependencies[] = entity
; Migrate handler.
files[] = MigrateBlockReferenceFieldHandler.inc
; Information added by Drupal.org packaging script on 2018-02-15
version = "7.x-2.7"
core = "7.x"
project = "blockreference"
datestamp = "1518674589"
<?php
/**
* @file
* Block Reference install & updates.
*/
/**
* Convert bid storage to module/delta storage.
*/
function blockreference_update_7200() {
$old_schema = drupal_get_schema(NULL, TRUE);
// Find fields & tables.
$all_fields = field_info_field_map();
$alter_tables = array();
foreach ($all_fields as $field_name => $field) {
if ($field['type'] == 'blockreference') {
$table_name = 'field_data_' . $field_name;
$alter_tables[$table_name] = $field_name;
$revision_table_name = 'field_revision_' . $field_name;
if (isset($old_schema[$revision_table_name])) {
$alter_tables[$revision_table_name] = $field_name;
}
}
}
$col_spec = array(
'type' => 'varchar',
'length' => 129, // 4 x 32 + 1 should be enough
'not null' => TRUE,
'default' => '',
);
$invalid_bids = array();
foreach ($alter_tables as $table_name => $field_name) {
$column_bid = $field_name . '_bid';
$column_moddelta = $field_name . '_moddelta';
// Alter table columns.
db_add_field($table_name, $column_moddelta, $col_spec);
// Convert bids to moddeltas.
db_query("
UPDATE {{$table_name}}
SET
$column_moddelta = (SELECT CONCAT(module, ':', delta) FROM {block} WHERE bid = {{$table_name}}.$column_bid)
WHERE
$column_bid IN (SELECT bid FROM {block})
")->execute();
$invalid_bids = array_merge(
$invalid_bids,
array_values(db_query("
SELECT $column_bid
FROM {$table_name}
WHERE $column_moddelta = ''
")->fetchCol(0, 0))
);
// Alter table columns.
db_drop_field($table_name, $column_bid);
// Alter table columns.
db_add_index($table_name, 'moddelta', array($column_moddelta));
}
if ($invalid_bids) {
$invalid_bids = array_unique($invalid_bids);
drupal_set_message(format_string("@num block ids could not be converted, because they don't exist (anymore).", array(
'@num' => count($invalid_bids),
)), 'warning');
}
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment