We need a block for related content. It is like the Content block, but the difference is that the content block shows fixed content list, while the related block shows content based on the article on the current page.
Make sure you know about creating blocks first: http://integratedfordevelopers.com/content/documentation/article/create-a-block-with-integrated-block-bundle
Blocks can be loaded on all pages, but the Related content block load content based on the current loaded content (for example article). So this block can only be used on article detail pages (this is not validated, the block will simply not give any output if you use it elsewhere). For the test set-up you need to have an article page.
Steps for the test set-up:
- You should have loaded the datafixes first.
- Update your composer.json with these versions and do a composer update:
- After that add your local domain to a channel (if you have not already):
Go to Menu - Manage - Channels
Edit a channel (with the pencil icon)
For domains, enter: localhost
- Add a website to the channel (if you have not already):
Go to Menu - Manage - Connectors
Choose Options - New website configuration
form.config.name: Website 2
form.config.channels: the channel you have chosen
- Add an content item:
Go to Create new - Article
Enter some test content, the title is important, this will be the URL. Title: "Article page for block"
In the content add: [block id="my_first_related_block"]
- Develop your new block
- Go to /admin/block/ and create an instance of your block called My first related block
- Now you'll see your new block
- Title (will be available from the based content block by default I think)
- Published title (like in Content block)
- Type (select box):
Show items which have the current document linked
Show items which share linked items with the current document
(two other types will be added in a next release)
- Relation (select box, choose from Integrated relations as show at /admin/relation/)
- Sort by (select box, options: Publication date, more options will be added in a next release)
- Items per page (like in Content block)
- Max items (like in Content block)
- Layout (will be available from the based content block by default I think)
You need to create a BlockHandler for the block (kind of controller). In the BlockHandler you need to know the active document. You can do this with:
$document = $this->blockManager->getDocument();
For the type "Show items which have the current document linked" you have to query the content from mongodb. Please use the repository for this (no queries in controllers/blockhandlers/etc.). You need to query the content for relations.references.$id = $document.getId() AND relations.relationId = the relation as chosen in the block form.
Example situation: your article is linked to keywords "apple" and "banana". The block will show other content that also has the keywords "apple" and "banana" links.
First you have to find the ID's of the items that are linked to the current document with $document->getReferencesByRelationId($relationId) ($relationId as chosen from the form).
After that you need to query the content for relations.references.$id = one of the ids from the previous line AND relations.relationId = the relation as chosen in the block form.