Commit f499ccf7 authored by Tim Bozeman's avatar Tim Bozeman Committed by Tim Bozeman
Browse files

Issue #3281239 by Tim Bozeman, heddn: Use machine name format for library names

parent 5eb23bfc
Loading
Loading
Loading
Loading
+26 −2
Original line number Diff line number Diff line
@@ -233,11 +233,14 @@ final class ComponentLibraryAssetForm extends EntityForm {
      ];

      $form[$asset_type][$delta]['name'] = [
        '#type' => 'textfield',
        '#type' => 'machine_name',
        '#title' => $this->t('Library Name'),
        '#maxlength' => 255,
        '#default_value' => $values['name'],
        '#description' => $this->t('Name for the library asset file.'),
        '#description' => $this->t('Machine name for the library asset file.'),
        '#machine_name' => [
          'exists' => [$this, 'fileExists'],
        ],
      ];

      $form[$asset_type][$delta]['code'] = [
@@ -306,4 +309,25 @@ final class ComponentLibraryAssetForm extends EntityForm {
    $this->libraryDiscovery->clearCachedDefinitions();
  }

  /**
   * Machine name exists callback.
   *
   * Ensure that within the same library we don't duplicate file names.
   */
  public function fileExists($entity_id, array $element, FormStateInterface $form_state) {
    $files = $this->getFilesDefaultValue($element['#parents'][0], $form_state);
    if ($files) {
      $count = 0;
      foreach($files as $file) {
        if (!empty($file['name']) && $file['name'] === $entity_id) {
          $count++;
        }
      }
      if ($count > 1) {
        return TRUE;
      }
    }
    return FALSE;
  }

}