use views programmatically

Upload: sorindediu

Post on 14-Apr-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Use Views Programmatically

    1/26

    Use viewsprogrammatically

    Sorin Dediu

    Drupal developer

  • 7/30/2019 Use Views Programmatically

    2/26

    When we should override our view

    Some time when you have a non standard situation.

    You need to output views results in a way that Views

    cant natively.

    You have an exposed filter and it not work like you

    need.

    You need to override a view query.

    Something else.

  • 7/30/2019 Use Views Programmatically

    3/26

    Let's start, but how?

  • 7/30/2019 Use Views Programmatically

    4/26

    hook_views_api()

    The first thing you have to do is tell Views that your module is

    going to using the Views API

  • 7/30/2019 Use Views Programmatically

    5/26

    hook_views_api()

    Next we creating new file with name: mymodule.views.inc

    and it should be placed in our current module: includes/views/

    This line tells views to look in /includes/views/ and all hooks from this

    file will be auto-loaded by views module.

  • 7/30/2019 Use Views Programmatically

    6/26

    hook_views_api()

    What we can use in this file ?

  • 7/30/2019 Use Views Programmatically

    7/26

    hook_views_api()

    You can use here all hooks provided by views core module.

    http://api.drupal.org/api/views/views.api.php/

    http://api.drupal.org/api/views/views.api.php/http://api.drupal.org/api/views/views.api.php/http://api.drupal.org/api/views/views.api.php/
  • 7/30/2019 Use Views Programmatically

    8/26

    Lets see an easy way to call a view programmatically

    If you want to send an argument into the views contextual filter,simply add another argument to the views_embed_view function.

  • 7/30/2019 Use Views Programmatically

    9/26

    Lets see an easy way to call a view programmatically

    Here can be any other argument, nid, title, date for example: if

    you use a date as a contextual filter the allowed type of format date

    is YYYY-MM-DD.

  • 7/30/2019 Use Views Programmatically

    10/26

    Small Tip

    Why we should to rename views display

    machine name ?

  • 7/30/2019 Use Views Programmatically

    11/26

    Small Tip

    Will help you with naming views template files

    Help developers when using viewsprogrammatically

    Help site maintainers to maintain a view with

    multiple displays Help themer to easy theme the view

  • 7/30/2019 Use Views Programmatically

    12/26

    views_get_view()

    Get a view from the database or from default

    views.

    This function is just a static wrapper around

    views::load(). This function isn't called

    'views_load()' primarily because it might get a

    view from the default views which aren't

    technically loaded from the database.

  • 7/30/2019 Use Views Programmatically

    13/26

    Calling the View

    Just call the view to make sure it work

  • 7/30/2019 Use Views Programmatically

    14/26

    Calling the View

    By default, views_get_view() returns the default display, but we

    need to return the block display. In this case, the display that we

    have is the first block, so its key was block_1.

  • 7/30/2019 Use Views Programmatically

    15/26

    Filtering Results

    Lets add a filter and filtering rows based on the $delta value. Views

    prefers the date in a machine readable format, according to the

    UI.

  • 7/30/2019 Use Views Programmatically

    16/26

    hook_views_query_alter()

    Allow to alter the query before executing the query. To pass

    variables from exposed filters in query or join new table to the

    views query.

  • 7/30/2019 Use Views Programmatically

    17/26

    hook_views_query_alter()

  • 7/30/2019 Use Views Programmatically

    18/26

    hook_views_pre_view()

    Allows altering a view at the very beginning of

    views processing, before anything is done. You

    can change here view $display_id or override

    view arguments.

  • 7/30/2019 Use Views Programmatically

    19/26

    hook_views_pre_view()

    Small example:

  • 7/30/2019 Use Views Programmatically

    20/26

    hook_views_pre_render()

    This hook is called right before the render

    process. The query has been executed, and

    the pre_render() phase has already happened

    for handlers, so all data should be available.

  • 7/30/2019 Use Views Programmatically

    21/26

    hook_views_pre_render()

  • 7/30/2019 Use Views Programmatically

    22/26

    hook_views_default_views()

    This hook allows modules to provide their

    own views which can either be used as-is or as

    a "starter" for users to build from. This hook

    should be placed in

    MODULENAME.views_default.inc

  • 7/30/2019 Use Views Programmatically

    23/26

    hook_views_default_views()

    You just need to add your exported view here

    and it will be used as default.

  • 7/30/2019 Use Views Programmatically

    24/26

    Want to see more ?Try it yourself ;)

    http://api.drupal.org/api/views/views.api.php/

    http://api.drupal.org/api/views/views.api.php/http://api.drupal.org/api/views/views.api.php/http://api.drupal.org/api/views/views.api.php/
  • 7/30/2019 Use Views Programmatically

    25/26

    Questions ?

  • 7/30/2019 Use Views Programmatically

    26/26

    Thanks you

    [email protected]