class YamlFormTemplatesController in YAML Form 8
Provides route responses for form templates.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\yamlform_templates\Controller\YamlFormTemplatesController implements ContainerInjectionInterface
Expanded class hierarchy of YamlFormTemplatesController
File
- modules/
yamlform_templates/ src/ Controller/ YamlFormTemplatesController.php, line 21
Namespace
Drupal\yamlform_templates\ControllerView source
class YamlFormTemplatesController extends ControllerBase implements ContainerInjectionInterface {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The form builder.
*
* @var \Drupal\Core\Form\FormBuilderInterface
*/
protected $formBuilder;
/**
* Form storage.
*
* @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
*/
protected $yamlformStorage;
/**
* Constructs a YamlFormTemplatesController object.
*
* @param \Drupal\Core\Session\AccountInterface $current_user
* Current user.
* @param \Drupal\Core\Form\FormBuilderInterface $form_builder
* The form builder.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity manager.
*/
public function __construct(AccountInterface $current_user, FormBuilderInterface $form_builder, EntityTypeManagerInterface $entity_type_manager) {
$this->currentUser = $current_user;
$this->formBuilder = $form_builder;
$this->yamlformStorage = $entity_type_manager
->getStorage('yamlform');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('current_user'), $container
->get('form_builder'), $container
->get('entity_type.manager'));
}
/**
* Returns the form templates index page.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
*
* @return array|RedirectResponse
* A render array representing the form templates index page or redirect
* response to a selected form via the filter's autocomplete.
*/
public function index(Request $request) {
$keys = $request
->get('search');
// Handler autocomplete redirect.
if ($keys && preg_match('#\\(([^)]+)\\)$#', $keys, $match)) {
if ($yamlform = $this->yamlformStorage
->load($match[1])) {
return new RedirectResponse($yamlform
->toUrl()
->setAbsolute(TRUE)
->toString());
}
}
$header = [
$this
->t('Title'),
[
'data' => $this
->t('Description'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
[
'data' => $this
->t('Operations'),
'colspan' => 2,
],
];
$yamlforms = $this
->getTemplates($keys);
$rows = [];
foreach ($yamlforms as $yamlform) {
$route_parameters = [
'yamlform' => $yamlform
->id(),
];
$row['title'] = $yamlform
->toLink();
$row['description']['data']['description']['#markup'] = $yamlform
->get('description');
if ($this->currentUser
->hasPermission('create yamlform')) {
$row['select']['data'] = [
'#type' => 'operations',
'#links' => [
'duplicate' => [
'title' => $this
->t('Select'),
'url' => Url::fromRoute('entity.yamlform.duplicate_form', $route_parameters),
'attributes' => YamlFormDialogHelper::getModalDialogAttributes(640),
],
],
];
}
$row['preview']['data'] = [
'#type' => 'operations',
'#links' => [
'preview' => [
'title' => $this
->t('Preview'),
'url' => Url::fromRoute('entity.yamlform.preview', $route_parameters),
'attributes' => YamlFormDialogHelper::getModalDialogAttributes(800),
],
],
];
$rows[] = $row;
}
$build = [];
$build['filter_form'] = $this->formBuilder
->getForm('\\Drupal\\yamlform_templates\\Form\\YamlFormTemplatesFilterForm', $keys);
$build['table'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => $this
->t('There are no templates available.'),
'#cache' => [
'contexts' => $this->yamlformStorage
->getEntityType()
->getListCacheContexts(),
'tags' => $this->yamlformStorage
->getEntityType()
->getListCacheTags(),
],
];
// Must preload libraries required by (modal) dialogs.
$build['#attached']['library'][] = 'yamlform/yamlform.admin.dialog';
return $build;
}
/**
* Returns a form to add a new submission to a form.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
* @param \Drupal\yamlform\YamlFormInterface $yamlform
* The form this submission will be added to.
*
* @return array|NotFoundHttpException
* The form submission form.
*/
public function previewForm(Request $request, YamlFormInterface $yamlform) {
if (!$yamlform
->isTemplate()) {
return new NotFoundHttpException();
}
return $yamlform
->getSubmissionForm([], 'preview');
}
/**
* Get form templates.
*
* @param string $keys
* (optional) Filter templates by key word.
*
* @return array|\Drupal\Core\Entity\EntityInterface[]
* An array form entity that are used as templates.
*/
protected function getTemplates($keys = '') {
$query = $this->yamlformStorage
->getQuery();
$query
->condition('template', TRUE);
// Filter by key(word).
if ($keys) {
$or = $query
->orConditionGroup()
->condition('title', $keys, 'CONTAINS')
->condition('description', $keys, 'CONTAINS')
->condition('elements', $keys, 'CONTAINS');
$query
->condition($or);
}
$query
->sort('title');
$entity_ids = $query
->execute();
if (empty($entity_ids)) {
return [];
}
/* @var $entities \Drupal\yamlform\YamlFormInterface[] */
$entities = $this->yamlformStorage
->loadMultiple($entity_ids);
// If the user is not a form admin, check view access to each form.
if (!$this
->isAdmin()) {
foreach ($entities as $entity_id => $entity) {
if (!$entity
->access('view')) {
unset($entities[$entity_id]);
}
}
}
return $entities;
}
/**
* Route preview title callback.
*
* @param \Drupal\yamlform\YamlFormInterface|null $yamlform
* A form.
*
* @return string
* The form label.
*/
public function previewTitle(YamlFormInterface $yamlform = NULL) {
return $this
->t('Previewing @title template', [
'@title' => $yamlform
->label(),
]);
}
/**
* Is the current user a form administrator.
*
* @return bool
* TRUE if the current user has 'administer yamlform' or 'edit any yamlform'
* permission.
*/
protected function isAdmin() {
return $this->currentUser
->hasPermission('administer yamlform') || $this->currentUser
->hasPermission('edit any yamlform');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. | |
YamlFormTemplatesController:: |
protected | property |
The current user. Overrides ControllerBase:: |
|
YamlFormTemplatesController:: |
protected | property |
The form builder. Overrides ControllerBase:: |
|
YamlFormTemplatesController:: |
protected | property | Form storage. | |
YamlFormTemplatesController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
YamlFormTemplatesController:: |
protected | function | Get form templates. | |
YamlFormTemplatesController:: |
public | function | Returns the form templates index page. | |
YamlFormTemplatesController:: |
protected | function | Is the current user a form administrator. | |
YamlFormTemplatesController:: |
public | function | Returns a form to add a new submission to a form. | |
YamlFormTemplatesController:: |
public | function | Route preview title callback. | |
YamlFormTemplatesController:: |
public | function | Constructs a YamlFormTemplatesController object. |