summaryrefslogtreecommitdiff
path: root/modules/node
diff options
context:
space:
mode:
Diffstat (limited to 'modules/node')
-rw-r--r--modules/node/node.install63
-rw-r--r--modules/node/node.module100
-rw-r--r--modules/node/node.test28
3 files changed, 188 insertions, 3 deletions
diff --git a/modules/node/node.install b/modules/node/node.install
index 473bc4efd..3a139e981 100644
--- a/modules/node/node.install
+++ b/modules/node/node.install
@@ -353,6 +353,34 @@ function node_schema() {
'primary key' => array('type'),
);
+ $schema['block_node_type'] = array(
+ 'description' => 'Sets up display criteria for blocks based on content types',
+ 'fields' => array(
+ 'module' => array(
+ 'type' => 'varchar',
+ 'length' => 64,
+ 'not null' => TRUE,
+ 'description' => "The block's origin module, from {block}.module.",
+ ),
+ 'delta' => array(
+ 'type' => 'varchar',
+ 'length' => 32,
+ 'not null' => TRUE,
+ 'description' => "The block's unique delta within module, from {block}.delta.",
+ ),
+ 'type' => array(
+ 'type' => 'varchar',
+ 'length' => 32,
+ 'not null' => TRUE,
+ 'description' => "The machine-readable name of this type from {node_type}.type.",
+ ),
+ ),
+ 'primary key' => array('module', 'delta', 'type'),
+ 'indexes' => array(
+ 'type' => array('type'),
+ ),
+ );
+
return $schema;
}
@@ -610,6 +638,41 @@ function node_update_7009() {
}
/**
+ * Add the {block_node_type} table.
+ */
+function node_update_7010() {
+ $schema['block_node_type'] = array(
+ 'description' => 'Sets up display criteria for blocks based on content types',
+ 'fields' => array(
+ 'module' => array(
+ 'type' => 'varchar',
+ 'length' => 64,
+ 'not null' => TRUE,
+ 'description' => "The block's origin module, from {block}.module.",
+ ),
+ 'delta' => array(
+ 'type' => 'varchar',
+ 'length' => 32,
+ 'not null' => TRUE,
+ 'description' => "The block's unique delta within module, from {block}.delta.",
+ ),
+ 'type' => array(
+ 'type' => 'varchar',
+ 'length' => 32,
+ 'not null' => TRUE,
+ 'description' => "The machine-readable name of this type from {node_type}.type.",
+ ),
+ ),
+ 'primary key' => array('module', 'delta', 'type'),
+ 'indexes' => array(
+ 'type' => array('type'),
+ ),
+ );
+
+ db_create_table('block_node_type', $schema['block_node_type']);
+}
+
+/**
* @} End of "defgroup updates-6.x-to-7.x"
* The next series of updates should start at 8000.
*/
diff --git a/modules/node/node.module b/modules/node/node.module
index e0e159073..0325f5c0f 100644
--- a/modules/node/node.module
+++ b/modules/node/node.module
@@ -2190,6 +2190,8 @@ function theme_node_recent_content($variables) {
* Implements hook_form_FORMID_alter().
*
* Adds node-type specific visibility options to add block form.
+ *
+ * @see block_add_block_form()
*/
function node_form_block_add_block_form_alter(&$form, &$form_state) {
node_form_block_admin_configure_alter($form, $form_state);
@@ -2199,6 +2201,8 @@ function node_form_block_add_block_form_alter(&$form, &$form_state) {
* Implements hook_form_FORMID_alter().
*
* Adds node-type specific visibility options to block configuration form.
+ *
+ * @see block_admin_configure()
*/
function node_form_block_admin_configure_alter(&$form, &$form_state) {
$default_type_options = db_query("SELECT type FROM {block_node_type} WHERE module = :module AND delta = :delta", array(
@@ -2220,13 +2224,15 @@ function node_form_block_admin_configure_alter(&$form, &$form_state) {
'#options' => node_type_get_names(),
'#description' => t('Show this block only on pages that display content of the given type(s). If you select no types, there will be no type-specific limitation.'),
);
- $form['#submit'][] = 'node_block_admin_configure_submit';
+ $form['#submit'][] = 'node_form_block_admin_configure_submit';
}
/**
* Form submit handler for block configuration form.
+ *
+ * @see node_form_block_admin_configure_alter()
*/
-function node_block_admin_configure_submit($form, &$form_state) {
+function node_form_block_admin_configure_submit($form, &$form_state) {
db_delete('block_node_type')
->condition('module', $form_state['values']['module'])
->condition('delta', $form_state['values']['delta'])
@@ -2243,6 +2249,96 @@ function node_block_admin_configure_submit($form, &$form_state) {
}
/**
+ * Implements hook_form_FORMID_alter().
+ *
+ * Adds node specific submit handler to delete custom block form.
+ *
+ * @see block_custom_block_delete()
+ */
+function node_form_block_custom_block_delete_alter(&$form, &$form_state) {
+ $form['#submit'][] = 'node_form_block_custom_block_delete_submit';
+}
+
+/**
+ * Form submit handler for custom block delete form.
+ *
+ * @see node_form_block_custom_block_delete_alter()
+ */
+function node_form_block_custom_block_delete_submit($form, &$form_state) {
+ db_delete('block_node_type')
+ ->condition('module', 'block')
+ ->condition('delta', $form_state['values']['bid'])
+ ->execute();
+}
+
+/**
+ * Implements hook_modules_uninstalled().
+ *
+ * Cleanup {block_node_type} table from modules' blocks.
+ */
+function node_modules_uninstalled($modules) {
+ db_delete('block_node_type')
+ ->condition('module', $modules, 'IN')
+ ->execute();
+}
+
+/**
+ * Implements hook_block_info_alter().
+ *
+ * Check the content type specific visibilty settings.
+ * Remove the block if the visibility conditions are not met.
+ */
+function node_block_info_alter(&$blocks) {
+ global $theme_key;
+
+ // Build an array of node types for each block.
+ $block_node_types = array();
+ $result = db_query('SELECT module, delta, type FROM {block_node_type}');
+ foreach ($result as $record) {
+ $block_node_types[$record->module][$record->delta][$record->type] = TRUE;
+ }
+
+ $node = menu_get_object();
+ $node_types = node_type_get_types();
+ if (arg(0) == 'node' && arg(1) == 'add' && arg(2)) {
+ $node_add_arg = strtr(arg(2), '-', '_');
+ }
+ foreach ($blocks as $key => $block) {
+ if (!isset($block->theme) || !isset($block->status) || $block->theme != $theme_key || $block->status != 1) {
+ // This block was added by a contrib module, leave it in the list.
+ continue;
+ }
+
+ // If a block has no node types associated, it is displayed for every type.
+ // For blocks with node types associated, if the node type does not match
+ // the settings from this block, remove it from the block list.
+ if (isset($block_node_types[$block->module][$block->delta])) {
+ if (!empty($node)) {
+ // This is a node or node edit page.
+ if (!isset($block_node_types[$block->module][$block->delta][$node->type])) {
+ // This block should not be displayed for this node type.
+ unset($blocks[$key]);
+ continue;
+ }
+ }
+ elseif (isset($node_add_arg) && isset($node_types[$node_add_arg])) {
+ // This is a node creation page
+ if (!isset($block_node_types[$block->module][$block->delta][$node_add_arg])) {
+ // This block should not be displayed for this node type.
+ unset($blocks[$key]);
+ continue;
+ }
+ }
+ else {
+ // This is not a node page, remove the block.
+ unset($blocks[$key]);
+ continue;
+ }
+ }
+ }
+}
+
+/**
* A generic function for generating RSS feeds from a set of nodes.
*
* @param $nids
diff --git a/modules/node/node.test b/modules/node/node.test
index c302d9bca..b7ae58a91 100644
--- a/modules/node/node.test
+++ b/modules/node/node.test
@@ -1289,7 +1289,7 @@ class NodeBlockFunctionalTest extends DrupalWebTestCase {
}
function setUp() {
- parent::setUp('node');
+ parent::setUp('node', 'block');
// Create users and test node.
$this->admin_user = $this->drupalCreateUser(array('administer content types', 'administer nodes', 'administer blocks'));
@@ -1379,6 +1379,32 @@ class NodeBlockFunctionalTest extends DrupalWebTestCase {
$this->assertText($node2->title, t('Node found in block.'));
$this->assertText($node3->title, t('Node found in block.'));
$this->assertText($node4->title, t('Node found in block.'));
+
+ // Create the custom block.
+ $custom_block = array();
+ $custom_block['info'] = $this->randomName();
+ $custom_block['title'] = $this->randomName();
+ $custom_block['types[article]'] = TRUE;
+ $custom_block['body[value]'] = $this->randomName(32);
+ $custom_block['regions[garland]'] = 'content';
+ $custom_block['regions[seven]'] = 'content';
+ $this->drupalPost('admin/structure/block/add', $custom_block, t('Save block'));
+
+ $bid = db_query("SELECT bid FROM {block_custom} WHERE info = :info", array(':info' => $custom_block['info']))->fetchField();
+ $this->assertTrue($bid, t('Custom block with visibility rule was created.'));
+
+ // Verify visibility rules.
+ $this->drupalGet('');
+ $this->assertNoText($custom_block['title'], t('Block was displayed on the front page.'));
+ $this->drupalGet('node/add/article');
+ $this->assertText($custom_block['title'], t('Block was displayed on the node/add/article page.'));
+ $this->drupalGet('node/' . $node1->nid);
+ $this->assertText($custom_block['title'], t('Block was displayed on the node/N.'));
+
+ // Delete the created custom block & verify that it's been deleted.
+ $this->drupalPost('admin/structure/block/manage/block/' . $bid . '/delete', array(), t('Delete'));
+ $bid = db_query("SELECT 1 FROM {block_node_type} WHERE module = 'block' AND delta = :delta", array(':delta' => $bid))->fetchField();
+ $this->assertFalse($bid, t('Custom block was deleted.'));
}
}
/**