You are here

public function Acquiadam::validate in Media: Acquia DAM 8

File

src/Plugin/EntityBrowser/Widget/Acquiadam.php, line 697

Class

Acquiadam
Uses a view to provide entity listing in a browser's widget.

Namespace

Drupal\media_acquiadam\Plugin\EntityBrowser\Widget

Code

public function validate(array &$form, FormStateInterface $form_state) {

  // If the primary submit button was clicked to select assets.
  if (!empty($form_state
    ->getTriggeringElement()['#eb_widget_main_submit'])) {

    // The media bundle.
    $media_bundle = $this->entityTypeManager
      ->getStorage('media_type')
      ->load($this->configuration['media_type']);

    // Load the field definitions for this bundle.
    $field_definitions = $this->entityFieldManager
      ->getFieldDefinitions('media', $media_bundle
      ->id());

    // Load the file settings to validate against.
    $field_map = $media_bundle
      ->getFieldMap();
    if (!isset($field_map['file'])) {
      $message = $this
        ->t('Missing file mapping. Check your media configuration.');
      $form_state
        ->setError($form['widget']['asset-container']['assets'], $message);
      return;
    }
    $file_extensions = $field_definitions[$field_map['file']]
      ->getItemDefinition()
      ->getSetting('file_extensions');
    $supported_extensions = explode(',', preg_replace('/,?\\s/', ',', $file_extensions));

    // The form input uses checkboxes which returns zero for unchecked assets.
    // Remove these unchecked assets.
    $assets = array_filter($form_state
      ->getValue('assets'));

    // Fetch assets.
    $dam_assets = $this->acquiadam
      ->getAssetMultiple($assets);

    // Get the cardinality for the media field that is being populated.
    $field_cardinality = $form_state
      ->get([
      'entity_browser',
      'validators',
      'cardinality',
      'cardinality',
    ]);
    if (!count($assets)) {
      $form_state
        ->setError($form['widget']['asset-container'], $this
        ->t('Please select an asset.'));
    }

    // If the field cardinality is limited and the number of assets selected
    // is greater than the field cardinality.
    if ($field_cardinality > 0 && count($assets) > $field_cardinality) {

      // Format the error message for singular or plural
      // depending on cardinality.
      $message = $this
        ->formatPlural($field_cardinality, 'You can not select more than 1 entity.', 'You can not select more than @count entities.');

      // Set the error message on the form.
      $form_state
        ->setError($form['widget']['asset-container']['assets'], $message);
    }

    // If the asset's file type does not match allowed file types.
    foreach ($dam_assets as $asset) {
      $filetype = $asset->filetype;
      $type_is_supported = in_array($filetype, $supported_extensions);
      if (!$type_is_supported) {
        $message = $this
          ->t('Please make another selection. The "@filetype" file type is not one of the supported file types (@supported_types).', [
          '@filetype' => $filetype,
          '@supported_types' => implode(', ', $supported_extensions),
        ]);

        // Set the error message on the form.
        $form_state
          ->setError($form['widget']['asset-container']['assets'], $message);
      }
    }
  }
}