v3Working with modules
Querying modules
Assuming you have an article entity:
php$article = $container->get('wf_cms.repository.page_article')->find($idArticle);
you can find a module using
getRoledModule
method:php$epigraph = $article->getRoledModule('epigraph');
This will return a module object for the module that has wf-role="epigraph"
.
If the template allows more modules of that type:
html<p wf-module="wfed/body_text/paragraph" wf-role="paragraph" />
you can get an array of these modules with
getRoledModules
(note the "s" at the end)php$paragraphs = $article->getRoledModules('paragraph');
Given a composite module:
html<div wf-module="wfed/composite/module" wf-role="composite"> <div wf-module="wfed/body_text/paragraph" wf-role="caption"> </div> </div>
you can either get access to the entire composite module:
php$composite = $article->getRoledModule('composite');
but you can also get access to the caption module:
php$caption = $article->getRoledModule('composite.paragraph');
Module objects
The method above returns an instance of Wf\Bundle\CmsBaseBundle\Entity\Collection\Module\AbstractEditorModule
. Depending on the type of the module, this object can be one of the following:
TextEditorModule - for text modules. You can cast this module to string to obtain the text entered inside this module:
php$text = (string) $textModule; //or $text = $textModule . '';
ImageEditorModule - for image modules. You can cast this module to string to obtain the URL of the image selected by the editor for this module. Also, you can obtain an instance of the
Image
entity that was selected for that module:php$image = $imageModule->getImage();
VideoEditorModule - for video modules. You can cast this module to string to obtain the URL of the video selected by the editor for this module. Also, you can obtain an instance of the
Video
entity that was selected for that module:php$video = $videoModule->getVideo();
AudioEditorModule - for audio modules. You can cast this module to string to obtain the URL of the audio selected by the editor for this module. Also, you can obtain an instance of the
Audio
entity that was selected for that module:php$audio = $audioModule->getAudio();
PageCompositeEditorModule - for
wf-role="wfed/composite/page"
modules. You can obtain an instance of thePage
entity linked in this module:php$page = $pageModule->getPage();
CompositeEditorModule - for "generic" composite modules.
For each of these modules, you can access the HTML saved in the database:
$html = $module->getHtml();
Setting modules' HTML
You can set the HTML for a module by calling:
$article->setEpigraph('<p>Epigraph</p>');
This works for top level modules only - you can't call setCompositeCaption
to set the caption
module inside the composite
module. Instead, you call setComposite("HTML of the composite module")
. Read more in Importing composite module