. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /** * Implements hook_block_info(). * * This hook declares what blocks are provided by the module. */ function node_reference_block_block_info() { $blocks['related-content'] = array( 'info' => t('Related information'), 'status' => TRUE, 'region' => BLOCK_REGION_NONE, 'visibility' => BLOCK_VISIBILITY_LISTED, 'pages' => 'node/*', ); return $blocks; } /** * Implements hook_block_configure(). */ function node_reference_block_block_configure($delta = '') { if ($delta === 'related-content') { $form['visibility']['exclude_related_type'] = array( '#type' => 'fieldset', '#title' => t('Excluded related types'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#group' => 'visibility', '#weight' => 6, '#attached' => array( 'js' => array(drupal_get_path('module', 'node_reference_block') . '/node_reference_block.js'), ), ); $form['visibility']['exclude_related_type']['related_content_block_exclude'] = array( '#type' => 'checkboxes', '#title' => t('Excluded related types'), '#default_value' => variable_get('related_content_block_exclude', array('pairing')), '#options' => node_type_get_names(), '#description' => t('Excludes selected types from related information.'), ); return $form; } } /** * Implements hook_block_save(). */ function node_reference_block_block_save($delta = '', $edit = array()) { if ($delta === 'related-content') { variable_set('related_content_block_exclude', array_filter($edit['related_content_block_exclude'])); } } /** * Implements hook_block_view(). * * This hook generates the contents of the blocks themselves. */ function node_reference_block_block_view($delta = '') { //The $delta parameter tells us which block is being requested. switch ($delta) { case 'related-content': return _node_reference_block_related_content_block_content(); default: return; } } /** * Related content block content. */ function _node_reference_block_related_content_block_content() { $exclude_types = variable_get('related_content_block_exclude', array('pairing')); if (strpos($_GET['q'], 'node/') === 0) { $node_path = explode('/', $_GET['q']); if (array_key_exists(1, $node_path) && is_numeric($node_path[1])) { $node = node_load($node_path[1]); $related_content = array(); // look for nodes refering the current node $query = db_select('field_config', 'f') ->fields('f', array('field_name')) ->condition('f.type', 'node_reference', '=') ->condition('f.deleted', FALSE, '='); $result = $query->execute(); while ($record = $result->fetchAssoc()) { $field_name = $record['field_name']; $q = db_select('field_data_' . $field_name, 'f') ->fields('f', array('entity_id')) ->condition('f.deleted', FALSE, '=') ->condition('f.' . $field_name . '_nid', $node->nid, '='); $res = $q->execute(); while ($rec = $res->fetchAssoc()) { $related_node = node_load($rec['entity_id']); if (in_array($related_node->type, $exclude_types)) continue; if (node_access('view', $related_node)) { $node_type = node_type_load($related_node->type); if (!array_key_exists($node_type->name, $related_content)) { $related_content[$node_type->name] = array(); } $related_content[$node_type->name][$related_node->title] = $related_node->nid; } } } // look for nodes referred by the current node $query = db_select('field_config', 'f'); $query->join('field_config_instance', 'fi', 'f.id = fi.field_id'); $query->fields('f', array('field_name')) ->condition('f.type', 'node_reference', '=') ->condition('f.deleted', FALSE, '=') ->condition('fi.bundle', $node->type, '='); $result = $query->execute(); while ($record = $result->fetchAssoc()) { $field_name = $record['field_name']; $field = $node->$field_name; $field_info = field_info_instance('node', $field_name, $node->type); $related_content[$field_info['label']] = array(); if (array_key_exists(LANGUAGE_NONE, $field)) { $values = $field[LANGUAGE_NONE]; if ($values) { foreach ($values as $val) { $related_node = node_load($val['nid']); // displays only published related content (language independent) if (node_access('view', $related_node) && $related_node->status == NODE_PUBLISHED) { $related_content[$field_info['label']][$related_node->title] = $related_node->nid; } } } } } if (empty($related_content)) { return; } $content = '