1

In Magento 1.x you could have dynamic comments on system->configuration fields by declaring your field in system.xml like this:

<field_code_here translate="label">
 <label>Label Here</label>
 <frontend_type>text</frontend_type>
 <sort_order>10</sort_order>
 <show_in_default>1</show_in_default>
 <show_in_website>1</show_in_website>
 <show_in_store>1</show_in_store>
 <comment>
 <model>[module]/field_comment</model><!-- this made it dynamic -->
 </comment>
</field_code_here>

Then you just need to create the [Namespace]/[Module]/Model/Field/Comment.php file with this content:

class [Namespace]_[Module]_Model_Field_Comment 
{
 public function getCommentText() //the method has to be named getCommentText
 {
 //do some calculations here
 return 'Some string based on the calculations';
 }
}

This way the field would have as comment what the getCommentText method from above returned.
Is there an alternative for Magento 2? It seams that I cannot attach a model to a comment tag. The system.xml is not validated by Magento/Backend/etc/system_file.xsd if I do.

Fabian Schmengler
66.2k25 gold badges191 silver badges422 bronze badges
asked Nov 24, 2014 at 14:30
8
  • Can you cite a use case? Commented Nov 24, 2014 at 15:09
  • @benmarks For Magento 1, I got this from EE1.13. I hope I'm allowed to show it. It's from the system.xml for the Enterprise_SalesArchive module <comment><model>enterprise_salesarchive/system_config_backend_active</model></comment>. because of this comment model I would see in the system->configuration the number of archived orders. I've used this approach on numerous custom extensions and it worked on CE also. Obviously I cannot find a case in Magento 2. If I did I wouldn't be asking this question. I would just replicate that case. Commented Nov 24, 2014 at 15:15
  • @benmarks. IN case you are wondering why I need it, I'm planning to clone this magento 1 extension github.com/tzyganu/stock-filter to magento 2, for learning purposes. And in that extension I used this comment model approach to show a message under a config field, depending on the value of an other config field Commented Nov 24, 2014 at 15:18
  • Hmm. I want to say that the comment field is static by design, and I wonder if this is a UX best practice. If there's variability based on another field, that should be part of the overall message. That said, I can understand your case. Might be worth a discussion or PR on our GitHub. Commented Nov 24, 2014 at 15:35
  • 4
    I'm voting to close this question as off-topic because this is related to a pre release version of Magento 2, more info here: meta.magento.stackexchange.com/questions/907/… Commented Apr 20, 2016 at 8:26

1 Answer 1

1

[EDIT]
In the latest magento version this is possible in a similar way as in M2. I fixed it :)

[Original Answer]

It seams that this feature is partially implemented in Magento 2. It doesn't work yet.
In the code that generates the configuration form there is the method Magento\Backend\Model\Config\Structure\Element\Field::getComment:

public function getComment($currentValue = '')
{
 $comment = '';
 if (isset($this->_data['comment'])) {
 if (is_array($this->_data['comment'])) {
 if (isset($this->_data['comment']['model'])) {
 $model = $this->_commentFactory->create($this->_data['comment']['model']);
 $comment = $model->getCommentText($currentValue);
 }
 } else {
 $comment = parent::getComment();
 }
 }
 return $comment;
}

This method should handle the case when the comment is generated by a model, but the validation schema does not allow a model tag inside the comment tag because the comment is defined like this:

<xs:element name="comment" type="xs:string" />

I already opened a ticked on github

answered Nov 25, 2014 at 8:23
3
  • Please see this PR for the instructions: github.com/magento/magento2/pull/2626 Commented Jun 2, 2017 at 20:44
  • I've seen that PR... Because I MADE IT :). Commented Jun 2, 2017 at 21:15
  • haha, I know :) just for the reference. Commented Jun 3, 2017 at 19:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.