3

I'm building a mobile app that has a list view which displays links to articles. The app's content is powered by Directus.

The list view has

title
description (200 characters, no HTML)
image

An article has

title
content (html)
image

To save the size of API requests, I'm storing the list view data in a collection called articles_list and articles in a collection called articles.

To keep Directus intuitive for the author, I need Directus to automatically create/update/delete an item in the articles_list collection for when an item is created/updated/deleted in the articles collection.

Both collections store basically the same data except that articles_list has description instead of content. The description field has the first 200 characters of content after the HTML tags have been removed.

I've had a look at the hooks documentation but I'm not sure how to create a new item in a different collection (articles_list). What's the best way of using an external library such as ezyang/htmlpurifier? How do I go about debugging my hook?

Any help would be greatly appreciated. I'm still learning to understand how Directus works.

neville
  • 474
  • 6
  • 8

2 Answers2

5

I would recommend creating one collection that has 4 fields:

title
excerpt (/ description)
content (html)
image

You can then use the fields parameter to only request the title and excerpt on your overview pages to save some bandwidth / latency:

/items/articles?fields=title,excerpt

It also saves you the hassle of having to keep two collections in sync, and it prevents having to store duplicate data

Rijk
  • 939
  • 7
  • 17
  • Thank you! But I would still need to remove the HTML tags and trim for excerpt which would need HTML purifier? Also could you answer the best way to debug a hook? – neville Mar 04 '19 at 22:31
  • 1
    No, I would recommend having a separate text-area next to the main HTML for the excerpt. That way you don't have to do any HTML clean ups (and you get the added bonus of being able to tweak the excerpt separately from the html. – Rijk Mar 04 '19 at 22:33
  • Debugging a hook is the same as debugging any other piece of PHP. I'm not a PHP developer myself, but I tend to var_dump() and die() my way to victory – Rijk Mar 04 '19 at 22:33
  • I thought it would be easier for the author if the excerpt was automatically taken from the content. But maybe too much for me to implement at this stage. I thought that might be the best way and I do something similar when I use Laravel but since hooks happen in the background, they don't normally output to the browser window or do they? – neville Mar 04 '19 at 22:37
0

This is an example for debug inside a hook enter image description here

And your question is here, if you want to change another collection field from hook the better way is to use ZendDB Example filter hook to update item with related field

Nemesius
  • 179
  • 10