Add constraint to multiple field and check them together

by golddragon007   Last Updated January 05, 2018 15:07 PM

I have a custom field type which contains two input fields. Now I need to add constraint to them which will check both is filled or not. I can check it with hook_entity_bundle_field_info_alter, but this way is ugly and it can slow down the site, if there's a lot of fields.

/**
 * Implements hook_entity_bundle_field_info_alter().
 */
function module_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
  /* @var Drupal\field\Entity\FieldConfig $field */
  foreach ($fields as $field) {
    if ($field->getType() === 'custom_field') {
      $field->addConstraint('all_or_none');
    }
  }
}

How can I add this constraint to the field (not to the input fields) inside the class?

Here's my custom field:

namespace Drupal\mymodule\Plugin\Field\FieldType;

use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;

/**
 * Provides a field type of tibbr.
 *
 * @FieldType(
 *   id = "custom_field",
 *   label = @Translation("Custom ID and Name fields"),
 *   module = "mymodule",
 *   default_formatter = "custom_field_formatter",
 *   default_widget = "custom_field_widget",
 * )
 */
class CustomField extends FieldItemBase implements FieldItemInterface {

  /**
   * Name field maximum length.
   *
   * @var int
   */
  private static $nameMaxLength = 100;

  /**
   * {@inheritdoc}
   */
  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['id'] = DataDefinition::create('integer')
      ->setLabel(t('ID'))
      ->setDescription(t('ID, value needs to be numeric unsinged.'))
      ->addConstraint('greater_than_0_integer');

    $properties['name'] = DataDefinition::create('string')
      ->setLabel(t('Name'))
      ->setDescription(t('Name, max %long character long.', ['%long' => self::$nameMaxLength]))
      ->addConstraint('max_length', ['maxLength' => self::$nameMaxLength]);

    return $properties;
  }

  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    $value = $this->get('id')->getValue();
    $value2 = $this->get('name')->getValue();
    return $value === NULL || $value === '' || $value2 === NULL || $value2 === '';
  }

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    return array(
      'columns' => array(
        'id' => array(
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => FALSE,
        ),
        'name' => array(
          'type' => 'varchar',
          'length' => self::$nameMaxLength,
          'not null' => FALSE,
        ),
      ),
    );
  }

}
Tags : 8 fields


Related Questions



How would I create conditional fields on install?

Updated October 13, 2016 09:03 AM


D8 - edit fields on display node

Updated March 05, 2018 22:07 PM