AD0-E717 practice question 14 of 15

What is the correct way to inject CMS block in a layout?

Full set: 77 questions
Answer options
A:

A. <block class="Magneto\Cms\Block\Block" name="blockidentifier"> <arguments> q <argumentname="block_id"xsi:type="string">my_cms_block_identifier</arguments></arguments>

Correct answer
B:

B. <block class="Magneto\Cms\Block\Block" name="blockidentifier"> q <actionmethod="setBlock">my_cms_block_identifier</action></block>

C:

C. <referenceBlock name="content"><block class="Magneto\Cms\Block\Block" name="block_identifier"identifier="my_cms_block_identifier"/></referenceBlock>

Correct Answer:
A. A. <block class="Magneto\Cms\Block\Block" name="blockidentifier"> <arguments> q <argumentname="block_id"xsi:type="string">my_cms_block_identifier</arguments></arguments>
Explanation

The correct method to inject a CMS block into a layout file is by declaring a block of class Magento\Cms\Block\Block and passing the unique block identifier as an argument. The node is the standard Magento 2 practice for passing data to a block's constructor from layout XML. The blockid argument name corresponds to the data key the Magento\Cms\Block\Block class uses to load the appropriate CMS block content from the database. This approach is declarative and aligns with modern Magento 2 development principles.

Why Incorrect

B. The tag is a deprecated method for calling public methods on a block instance. While it might work, it is not the recommended or correct modern approach; should be used for constructor injection.
C. Using a direct attribute like identifier="mycmsblockidentifier" in the tag is not a valid Magento 2 layout XML syntax. Block data must be passed via the sub-node.

References

1. Adobe Commerce Developer Guide, Layout XML instructions, Block arguments: This official documentation explicitly shows the use of the node to pass data to a block. It states, "Arguments are set for a block using the sub-node... The argument name corresponds to the parameter name in the constructor." This validates the structure used in option A.

Source: Adobe Commerce Developer Documentation > Frontend Developer Guide > Layouts > Layout XML instructions > Block arguments.

2. Adobe Commerce Developer Guide, Layout XML instructions, Common layout instructions: This section provides a direct example of how to add a CMS (static) block to a layout, which perfectly matches the syntax in option A.

Example Snippet: exampleblock

Source: Adobe Commerce Developer Documentation > Frontend Developer Guide > Layouts > Layout XML instructions > Common layout instructions.

3. Adobe Commerce Developer Guide, Layout XML instructions, Action: The documentation notes the deprecation of the tag for setting block properties. It explicitly recommends using the node instead, confirming that option B is not the correct or preferred method.

Source: Adobe Commerce Developer Documentation > Frontend Developer Guide > Layouts > Layout XML instructions > Action.

Scroll to Top