You are here

protected function WebformCliService::_drush_webform_validate in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Commands/WebformCliService.php \Drupal\webform\Commands\WebformCliService::_drush_webform_validate()

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

4 calls to WebformCliService::_drush_webform_validate()
WebformCliService::drush_webform_export_validate in src/Commands/WebformCliService.php
Implements drush_hook_COMMAND_validate().
WebformCliService::drush_webform_generate_validate in src/Commands/WebformCliService.php
Implements drush_hook_COMMAND_validate().
WebformCliService::drush_webform_import_validate in src/Commands/WebformCliService.php
WebformCliService::drush_webform_purge_validate in src/Commands/WebformCliService.php
Implements drush_hook_COMMAND_validate().

File

src/Commands/WebformCliService.php, line 1500

Class

WebformCliService
Drush version agnostic commands.

Namespace

Drupal\webform\Commands

Code

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