Created
April 4, 2024 16:59
-
-
Save ultimike/149a56e423c4f4587a2a93dfef9867ea to your computer and use it in GitHub Desktop.
UpdateRepositoriesForm with dependency injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Drupal\drupaleasy_repositories\Form; | |
use Drupal\Core\Entity\EntityTypeManagerInterface; | |
use Drupal\Core\Form\FormBase; | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\drupaleasy_repositories\DrupaleasyRepositoriesBatch; | |
use Drupal\drupaleasy_repositories\DrupaleasyRepositoriesService; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
/** | |
* Provides a DrupalEasy Repositories form. | |
*/ | |
final class UpdateRepositoriesForm extends FormBase { | |
/** | |
* The constructor. | |
* | |
* @param \Drupal\drupaleasy_repositories\DrupaleasyRepositoriesService $drupaleasyRepositoriesService | |
* The DrupalEasy repositories main service class. | |
* @param \Drupal\drupaleasy_repositories\DrupaleasyRepositoriesBatch $drupaleasyRepositoriesBatch | |
* The DrupalEasy repositories batch service class. | |
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager | |
* Drupal core's entity type manager service class. | |
*/ | |
public function __construct( | |
private readonly DrupaleasyRepositoriesService $drupaleasyRepositoriesService, | |
private readonly DrupaleasyRepositoriesBatch $drupaleasyRepositoriesBatch, | |
private readonly EntityTypeManagerInterface $entityTypeManager, | |
) {} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function create(ContainerInterface $container): UpdateRepositoriesForm { | |
return new static( | |
$container->get('drupaleasy_repositories.service'), | |
$container->get('drupaleasy_repositories.batch'), | |
$container->get('entity_type.manager'), | |
); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getFormId(): string { | |
return 'drupaleasy_repositories_update_repositories'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(array $form, FormStateInterface $form_state): array { | |
$form['message'] = [ | |
'#type' => 'textarea', | |
'#title' => $this->t('Message'), | |
'#required' => TRUE, | |
]; | |
$form['actions'] = [ | |
'#type' => 'actions', | |
'submit' => [ | |
'#type' => 'submit', | |
'#value' => $this->t('Send'), | |
], | |
]; | |
return $form; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function validateForm(array &$form, FormStateInterface $form_state): void { | |
// @todo Validate the form here. | |
// Example: | |
// @code | |
// if (mb_strlen($form_state->getValue('message')) < 10) { | |
// $form_state->setErrorByName( | |
// 'message', | |
// $this->t('Message should be at least 10 characters.'), | |
// ); | |
// } | |
// @endcode | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function submitForm(array &$form, FormStateInterface $form_state): void { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment