Embedding View in a node in Drupal and filtering it on array of nodes

Submitted by programmer on Sat, 11/24/2007 - 17:51.
::

 

<?php
$nid_array = array(8, 16);

print '<div class="PARENT_CHILDREN">';

// get the name of the view to embed as $view1
$view1 = views_get_view('testview');

//for ($i=0; $i<2; $i++){
 views_view_add_filter($view1, 'node', 'nid', 'OR', array('8', '16'), '');
// views_view_add_filter($view1, 'node', 'nid', 'OR', '16', '');
//}

 $view1->sort = array (
    array (
      'tablename' => 'node',
      'field' => 'created',
      'sortorder' => 'ASC',
      'options' => 'hour',
    ),
  );
 
  $view1->field = array (
    array (
      'tablename' => 'node',
      'field' => 'title',
      'label' => 'Title',
      'handler' => 'views_handler_field_nodelink_with_mark',
      'sortable' => '1',
      'options' => 'link',
    ),
    array (
      'tablename' => 'node_data_field_location',
      'field' => 'field_location_value',
      'label' => 'Address',
      'handler' => 'content_views_field_handler_group',
      'options' => 'default',
    ),
    array (
      'tablename' => 'node_data_field_property_type',
      'field' => 'field_property_type_value',
      'label' => 'Type',
      'handler' => 'content_views_field_handler_group',
      'options' => 'default',
    ),
    array (
      'tablename' => 'node_data_field_town_city',
      'field' => 'field_town_city_value',
      'label' => 'Town',
      'handler' => 'content_views_field_handler_group',
      'sortable' => '1',
      'options' => 'default',
    ),
    array (
      'tablename' => 'node_data_field_sale_or_rent',
      'field' => 'field_sale_or_rent_value',
      'label' => 'Status',
      'handler' => 'content_views_field_handler_group',
      'options' => 'default',
    ),
    array (
      'tablename' => 'node_data_field_area_ft',
      'field' => 'field_area_ft_value',
      'label' => 'Area',
      'handler' => 'content_views_field_handler_group',
      'sortable' => '1',
      'options' => 'default',
    ),
    array (
      'tablename' => 'node_data_field_price_rent',
      'field' => 'field_price_rent_value',
      'label' => 'Price/Rent',
      'handler' => 'content_views_field_handler_group',
      'sortable' => '1',
      'options' => 'default',
    ),
    array (
      'tablename' => 'node_data_field_price_rent_info',
      'field' => 'field_price_rent_info_value',
      'label' => 'Price Info',
      'handler' => 'content_views_field_handler_group',
      'options' => 'default',
    ),
    array (
      'tablename' => 'node_data_field_main_image',
      'field' => 'field_main_image_fid',
      'label' => 'Preview',
      'handler' => 'content_views_field_handler_group',
      'options' => '60x60',
    ),
  );
  $view->filter = array (
    array (
      'tablename' => 'node',
      'field' => 'type',
      'operator' => 'OR',
      'options' => '',
      'value' => array (
  0 => 'property',
),
    ),
    array (
      'tablename' => 'node_data_field_sale_or_rent',
      'field' => 'field_sale_or_rent_value_default',
      'operator' => 'OR',
      'options' => '',
      'value' => array (
  0 => '1',
  1 => '2',
),
    ),
    array (
      'tablename' => 'node',
      'field' => 'distinct',
      'operator' => '=',
      'options' => '',
      'value' => array (
  0 => 'distinct',
),
    ),
  );
  $view1->exposed_filter = array (
    array (
      'tablename' => 'node_data_field_sale_or_rent',
      'field' => 'field_sale_or_rent_value_default',
      'label' => 'Select Status',
      'optional' => '1',
      'is_default' => '0',
      'operator' => '1',
      'single' => '1',
    ),
  );
  $view1->requires = array(node, node_data_field_location, node_data_field_property_type, node_data_field_town_city, node_data_field_sale_or_rent, node_data_field_area_ft, node_data_field_price_rent, node_data_field_price_rent_info, node_data_field_main_image);

views_load_cache();
views_sanitize_view($view1);

// build the view
print views_build_view('embed', $view1, array(), true, 1);
print '</div>';?>

This might need a hack on views_query.inc at line 83:

 Replace the following code:

 if (!$filterinfo['field']) {
      $fieldbits = explode('.', $filter['field']);
      $filterinfo['field'] = $fieldbits[1];
    }

with: 

if (!$filterinfo['field']) {
    $fieldbits = explode('.', $filter['field']);
    // Next if statement added on 02/10/2007 by Ian Eldred
    // Fixes missing table name in filters added by code.
    if (isset($filter['table'])) {
      $filterinfo['table'] = $filter['table'];
    }
    else {
      $filterinfo['table'] = $fieldbits[0];
    }
    $filterinfo['field'] = $fieldbits[1];
  }

Post your Answer

  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options