Loading src/Form/ComponentLibraryAssetForm.php +26 −2 Original line number Diff line number Diff line Loading @@ -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'] = [ Loading Loading @@ -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; } } Loading
src/Form/ComponentLibraryAssetForm.php +26 −2 Original line number Diff line number Diff line Loading @@ -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'] = [ Loading Loading @@ -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; } }