1

I have a question about Drupal 7 and Views. Let's assume that I have views where I extract 2 articles ordered by post date. I want to extract only title and trimmed introduction (trim configured in field settings in view)

Now I would like to extract this data programmatically without HTML markup. So the result will be similar to this:

array(
    0 => array(
        'title' => 'Lorem ipsum',
        'introduction' => 'Lorem ipsum dolar solor...'
    ),
    1 => array(
        'title' => 'Lorem ipsum2',
        'introduction' => 'Lorem ipsum2 dolar solor...'
    )
)

Is it possible to extract such information from views? I tried to do something like this:

$view = views_get_view('view_name');
$view->set_display('display_id');
$view->execute();
$data = $view->result;

But $data contain two elements with full nodes (all additional fields that I don't want to grab from database)

In addition for instance introduction is grabbed fully (must be trimmed somewhere in PHP).

Is it possible to do something like this:

$view->applyFilters(); //Like trim etc
$data = $view->getResults(); //return title + trimmed introduction but without HTML markup

Do You have any tips how to solve this problem? Is it possible for instance to pass template that will be rendered (where I could display only raw result without any HTML tags) or something?

Any help would be appreciated:)

8
  • what do you indent to do with this result? Commented Oct 24, 2013 at 11:51
  • Anything, for instance return it via ajax as JSON
    – Krystian
    Commented Oct 24, 2013 at 11:52
  • Have you tried using strip html and trim on the views page when adding the fields? Commented Oct 24, 2013 at 11:56
  • Could You explain it a little more? Or maybe I don't clear it up enough. When views is rendered normally for instance $view->preview() it will generate something like this: <div class="views-field views-field-field-introduction"><div class="field-content">Lorem ipsum...</div></div> I don't want the outer html i just want Lorem ipsum...
    – Krystian
    Commented Oct 24, 2013 at 12:03
  • I don't get it - is there HTML in your fields, in their data in database, and you want to strip it, or something adds HTML formatting and you want to disable it?
    – Mołot
    Commented Oct 24, 2013 at 12:06

2 Answers 2

1

After suggestion from AlfredArmstrong I checked plugin style docs. To get filtered data without rendering html template simply do:

 $data = $view->style_plugin->render_fields($view->result);

Under $data will be array of filtered fields from view.

1
  • I feel like I've been searching all my life for this answer. THANK YOU!
    – Kiee
    Commented Mar 21, 2017 at 15:31
0

In your fields settings, open "Style settings:

Screenshot

Customize HTMLs, setting them both to -None- .

If that's not enough, Look at the right column. Section Other. Click on Theme information and provide custom tpl.php file for your field.

4
  • That's nice, it's actually give results without extra html markup. But is it possible to do it programmaticaly like in the code in my question? To get array of data instead of rendered view?
    – Krystian
    Commented Oct 24, 2013 at 12:53
  • @Astaz3l If you only want a query result, why don't you query directly? You can easily "steal" query from views.
    – Mołot
    Commented Oct 24, 2013 at 12:58
  • True, I can get the query directly but for instance it won't return trimmed content - it's done in PHP, not it MySQL query.
    – Krystian
    Commented Oct 24, 2013 at 13:01
  • @Astaz3l have you tried to (ab)use views_get_views_as_options?
    – Mołot
    Commented Oct 24, 2013 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.