3

I am trying to use getCriteria() along with buildElementsQuery () in my plugin in order to eventually modify the query. At the moment, I am not making any modifications to the query.

But when the the fields are outputted in the template, I ended up getting raw html on the screen. Not nicely formatted HTML. For example, the paragraph tag shows as <p> and in the source of the page it is &lt;p&gt;. The <p> should only be in the source.

How do I get the template to display the nice html and not the raw html?

In my plugin service

public function getMyLibrary()
{
    $criteria = craft()->elements->getCriteria(ElementType::Entry);
    $criteria->section = 'library';
    $criteria->limit = 10;

    return ($criteria->find());

}

In my template:

{% set my_library = craft.accd.my_library() %}

{% for entry in my_library %} 

    {{ entry.title }}
    {{ entry.body}}

{% endfor %}

Everything works as expected and the rich text html from the body displays correctly.

But when I change it to use buildElementsQuery as I will modifying the query, the entry.title and entry.body get displayed in the template as raw html.

Here is the code with the buildElementsQuery added. I had to also modify the field body to be field_body in the template.

In my plugin service

public function getMyLibrary()
{
    $criteria = craft()->elements->getCriteria(ElementType::Entry);
    $criteria->section = 'library';
    $criteria->limit = 10;

    $query = craft()->elements->buildElementsQuery($criteria);

    return ($query->queryAll());

}

In my template:

{% set my_library = craft.accd.my_library() %}

{% for entry in my_library %} 

    {{ entry.title }}
    {{ entry.field_body}}

{% endfor %}
7

$query->queryAll() returns raw arrays of data from the database, this is why you're needing to prepend field_ to each attribute, to get the column name.

You need to populate some EntryModels before you can use them properly in the templates, with something along the lines of:

$query = craft()->elements->buildElementsQuery($criteria);

// modify the query here

$results = $query->queryAll();

return EntryModel::populateModels($results);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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