Timeline for Constraint on paragraph field not working
Current License: CC BY-SA 4.0
9 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Sep 10, 2020 at 13:18 | comment | added | Baud |
It is not working: $value =$entity->my_paragraph_field->value; is not appropriate it should be $value = $entity->my_paragraph_field->entity->my_sub_field->value; as it is a paragraph. Note also that it is more easy to use the $items variable (no need to get the entity)... anyway, the issue is not there...
|
|
Sep 10, 2020 at 11:57 | comment | added | miststudent2011 | Were you able to get the field value printed in the constraint validate method ? First try to get the field value and you can apply validation once you have the variable for the field. | |
Sep 10, 2020 at 11:54 | comment | added | miststudent2011 | Constraint will be called everywhere as you have added it in field info alter. The problem here is constraint is not being applied because it can not get the value. Have you tried to execute my code ? In what places the paragraph is being used? If it is being used in a node just copy paste my code and execute it. Constraint will be applied. | |
Sep 10, 2020 at 10:58 | comment | added | Baud |
Sorry, but this doesn't work: $entity->hasField('my_paragraph_field') is useless because we have added the constraint to this field. Note that the code in the OP Is adding the violation in all cases (no test on a value) to make sure the problem is not in the code which is checking the value. The validate function create only the violation and display "I am displayed" to confirm that the function has been called. The issues are "Why the constraint message is not displayed" and "Why there is no validation failure"
|
|
Sep 10, 2020 at 9:07 | comment | added | miststudent2011 | @Baud I over looked your question, I have added the correct code now. You need to get the paragraph entity first and its fields to get the field value and later add validations to it. | |
Sep 10, 2020 at 9:05 | history | edited | miststudent2011 | CC BY-SA 4.0 |
added correct code.
|
Sep 10, 2020 at 7:27 | comment | added | Baud |
thank you for your help. Unfortunately it is not correct: $entity_type->id() is a Node, not a paragraph . But my_paragraph_field is referencing a paragraph. As you can see in my OP, the constraint is called (dpm("I am displayed") ) thus I don't think the problem is coming from mymodule_entity_bundle_field_info_alter
|
|
Sep 9, 2020 at 23:46 | history | edited | miststudent2011 | CC BY-SA 4.0 |
added 4 characters in body
|
Sep 9, 2020 at 23:41 | history | answered | miststudent2011 | CC BY-SA 4.0 |