
To enable an entity or field to be validated, Drupal needs to know which constraints apply to which field or entity type. Interacting With the Entity Field Validation API getInvalidValue() returns the value of the field that is in error.These align with the form structure if you're in the context of validating a form. For example, if the third tag in field_tags is in error, the property path might be field_tags.2.target_id. getPropertyPath() gets the name of the field in error.getMessage() gets the reason for the violation.filterByFieldAccess() filters only those violations the current user has access to edit.Īs the return implements \ArrayAccess, you can loop over the results and work with each ConstraintViolationInterface item in turn:.
Drupal rules reset value to null series#

If the resulting violations are empty, the entity is valid. That will give you an instance of Entit圜onstraintViolationListInterface which helpfully implements \Countable and \ArrayAccess. In Drupal 8, to check if an entity is valid, simply call its validate method:

Configurable fields are those added during site building using the Field API. Base fields are the fields that are largely fixed in entity type. Note that there are two broad types of fields: base fields and configurable fields. Perhaps a diagram might make this clearer. Even fields like the node title are lists, which just contain a single value.Įach FieldItemListInterface consists of one or more FieldItemInterface objects.Īt the lowest level of the API, each FieldItemInterface is comprised of one or more DataDefinitionInterface objects that make up the properties or columns in each item value. (Node, Comment, Taxonomy Term, and BlockContent, among others, implement this interface.)Įach field and each property on a content entity is an instance of FieldItemListInterface. Let’s start with ContentEntityInterface, the overarching interface that content entities in Drupal 8 implement. Key Interfaces in Drupal 8 Entity Field API When you work with fields and entities in Drupal 8, you’re likely to interact with a suite of interfaces that comprise the API. So in Drupal 8, you see consistency like this: In Drupal 8, the Entity Field API brings unified access to field properties and first-class objects for each entity type. For example, $node->title is a string, while $node->field_tags is an array.Īnd so, in Drupal 7 you might see things like this: In Drupal 7, entities are \StdClassobjects accessing field values depends on the entity and the field. It’s Like an Onion, or Maybe a Layer Cakeīut before we can talk about Drupal 8's Entity Validation API, we need to go over some background on the Entity API itself. The same scenario is repeated for Nodes, Users, Taxonomy terms, and custom Blocks (which aren’t entities per se in Drupal 7, but the story is the same).
Drupal rules reset value to null code#

Never heard of it? You're not alone – much of the fanfare around Drupal 8 is devoted to the shiny parts. Drupal 8 comes with plenty of new features: the high visibility ones, like CKEditor or Views in core, and those less obvious but equally pivotal to Drupal 8’s strength and flexibility, like the Entity Validation API.
