You are here

function _drush_yamlform_validate in YAML Form 8

Validate yamlform_id argument and source entity-type and entity-id options.

2 calls to _drush_yamlform_validate()
drush_yamlform_export_validate in drush/yamlform.drush.inc
Implements drush_hook_COMMAND_validate().
drush_yamlform_purge_validate in drush/yamlform.drush.inc
Implements drush_hook_COMMAND_validate().

File

drush/yamlform.drush.inc, line 519
YAML Form module drush commands.

Code

function _drush_yamlform_validate($yamlform_id = NULL) {
  if (empty($yamlform_id)) {
    return drush_set_error(dt('Form id required'));
  }
  if (!empty($yamlform_id) && !YamlForm::load($yamlform_id)) {
    return drush_set_error(dt('Form @id not recognized.', [
      '@id' => $yamlform_id,
    ]));
  }
  $entity_type = drush_get_option('entity-type');
  $entity_id = drush_get_option('entity-id');
  if ($entity_type || $entity_id) {
    if (empty($entity_type)) {
      return drush_set_error(dt('Entity type is required when entity id is specified.'));
    }
    if (empty($entity_id)) {
      return drush_set_error(dt('Entity id is required when entity type is specified.'));
    }
    $dt_args = [
      '@yamlform_id' => $yamlform_id,
      '@entity_type' => $entity_type,
      '@entity_id' => $entity_id,
    ];
    $source_entity = \Drupal::entityTypeManager()
      ->getStorage($entity_type)
      ->load($entity_id);
    if (!$source_entity) {
      return drush_set_error(dt('Unable to load @entity_type:@entity_id', $dt_args));
    }
    $dt_args['@title'] = $source_entity
      ->label();
    if (empty($source_entity->yamlform) || empty($source_entity->yamlform->target_id)) {
      return drush_set_error(dt("'@title' (@entity_type:@entity_id) does not reference a form.", $dt_args));
    }
    if ($source_entity->yamlform->target_id != $yamlform_id) {
      return drush_set_error(dt("'@title' (@entity_type:@entity_id) does not have a '@yamlform_id' yamlform associated with it.", $dt_args));
    }
  }
  return NULL;
}