WorkspaceTypeDeleteForm.php in Workspace 8
File
src/Entity/Form/WorkspaceTypeDeleteForm.php
View source
<?php
namespace Drupal\workspace\Entity\Form;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WorkspaceTypeDeleteForm extends EntityDeleteForm {
public $queryFactory;
public function __construct(QueryFactory $query_factory) {
$this->queryFactory = $query_factory;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.query'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$workspaces = $this->queryFactory
->get('workspace')
->condition('type', $this->entity
->id())
->execute();
if (!empty($workspaces)) {
$caption = '<p>' . $this
->formatPlural(count($workspaces), '%label is used by 1 workspace on your site. You can not remove this workspace type until you have removed all of the %label workspaces.', '%label is used by @count workspaces on your site. You may not remove %label until you have removed all of the %label custom workspaces.', [
'%label' => $this->entity
->label(),
]) . '</p>';
$form['description'] = [
'#markup' => $caption,
];
return $form;
}
else {
return parent::buildForm($form, $form_state);
}
}
}