You are here

public function EntityReferenceHandlerBase::validateHandlerSettings in CMS Content Sync 2.1.x

Same name and namespace in other branches
  1. 8 src/Plugin/EntityReferenceHandlerBase.php \Drupal\cms_content_sync\Plugin\EntityReferenceHandlerBase::validateHandlerSettings()
  2. 2.0.x src/Plugin/EntityReferenceHandlerBase.php \Drupal\cms_content_sync\Plugin\EntityReferenceHandlerBase::validateHandlerSettings()

Validate the settings defined above. $form and $form_state are the same as in the Form API.

Parameters

string $settings_key:

array $current_values:

Return value

mixed

Overrides FieldHandlerBase::validateHandlerSettings

File

src/Plugin/EntityReferenceHandlerBase.php, line 96

Class

EntityReferenceHandlerBase
Providing a base implementation for any reference field type.

Namespace

Drupal\cms_content_sync\Plugin

Code

public function validateHandlerSettings(array &$form, FormStateInterface $form_state, string $entity_type_name, string $bundle_name, string $field_name, $current_values) {
  if (!$this
    ->shouldPushReferencedEntities() && !$this
    ->shouldEmbedReferencedEntities()) {
    return;
  }
  $reference_types = $this
    ->getReferencedEntityTypes();
  foreach ($current_values['per_bundle_settings'] as $entity_type => $bundles) {
    foreach ($bundles as $bundle => $config) {
      $settings = $config['settings'];

      // Ignore ignored configs.
      if (Flow::HANDLER_IGNORE == $settings['handler']) {
        continue;
      }
      $index = array_search($entity_type, $reference_types);

      // Ignore configs that don't match our entity type.
      if (false === $index) {
        continue;
      }

      // This one has a push handler, so we can ignore it in further validation.
      unset($reference_types[$index]);
    }
  }

  // All referenced entities have a handler, so we're good.
  if (!count($reference_types)) {
    return;
  }

  // We are just about to load this element, so we don't have any form element available yet. Validation will be
  // triggered again when the form is submitted.
  if (empty($form[$this->entityTypeName][$this->bundleName]['properties'][$field_name]['handler'])) {
    return;
  }

  // No fitting handler was found- inform the user that he's missing some
  // configuration.
  if ($this
    ->forcePushingReferencedEntities() || $this
    ->forceEmbeddingReferencedEntities()) {
    $element =& $form[$this->entityTypeName][$this->bundleName]['properties'][$field_name]['handler'];
  }
  else {
    $element =& $form[$this->entityTypeName][$this->bundleName]['properties'][$field_name]['handler_settings']['export_referenced_entities'];
  }
  foreach ($reference_types as $type) {
    $form_state
      ->setError($element, t('You want to push %referenced\'s that are referenced in %source automatically, but you have not defined any handler for this entity type. Please scroll to the bundles of this entity type, add a handler and set "push" to "referenced" there.', [
      '%referenced' => $type,
      '%source' => $entity_type_name . '.' . $bundle_name . '.' . $field_name,
    ]));
  }
}