Embed views in custom module

1. Embed code to module

<?php

/**

 * Implementation of hook_views_api.

 */

function modulename_views_views_api() {

  return array(

    'api' => 2,

    'path' => drupal_get_path('module', 'modulename'),

  );

}

2. Embed exported views to modulename.views_default.inc

<?php

/**

 * Implementation of hook_views_default_views().

 */

function modulename_views_default_views() {

  /*Exported views code paste here*/

 

  $views[$view->name] = $view;

  return $views;

}

+ More things: Overriding views from other module

1. Define override views code

<?php

/**

* Implementation of hook_views_default_views_alter().

*/

function modulename_views_default_views_alter(&$views) {   

  module_load_include('inc', 'modulename', 'modulename.views_default_override');

  $new_views = modulename_views_default_override_views();

  foreach($new_views as $key => $view) {

    if(array_key_exists($key, $views)) {

      if(is_object($view)){        

        $views[$key] = $view;

      }

    }

  }

}

2. Embed exported changed views to modulename.views_default_override.inc

<?php

 

function modulename_views_default_override_views() {

  /*Exported views code paste here*/

  

  $views[$view->name] = $view;

  return $views;

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: