class ShortcutForm
Same name and namespace in other branches
- 11.x core/modules/shortcut/src/ShortcutForm.php \Drupal\shortcut\ShortcutForm
Form handler for the shortcut entity forms.
@internal
Hierarchy
- class \Drupal\Core\Form\FormBase extends \Drupal\Core\Form\FormInterface, \Drupal\Core\DependencyInjection\ContainerInjectionInterface uses \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Logger\LoggerChannelTrait, \Drupal\Core\Messenger\MessengerTrait, \Drupal\Core\Routing\RedirectDestinationTrait, \Drupal\Core\StringTranslation\StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm extends \Drupal\Core\Entity\EntityFormInterface implements \Drupal\Core\Form\FormBase
- class \Drupal\Core\Entity\ContentEntityForm extends \Drupal\Core\Entity\ContentEntityFormInterface implements \Drupal\Core\Entity\EntityForm
- class \Drupal\shortcut\ShortcutForm implements \Drupal\Core\Entity\ContentEntityForm
- class \Drupal\Core\Entity\ContentEntityForm extends \Drupal\Core\Entity\ContentEntityFormInterface implements \Drupal\Core\Entity\EntityForm
- class \Drupal\Core\Entity\EntityForm extends \Drupal\Core\Entity\EntityFormInterface implements \Drupal\Core\Form\FormBase
Expanded class hierarchy of ShortcutForm
File
-
core/
modules/ shortcut/ src/ ShortcutForm.php, line 14
Namespace
Drupal\shortcutView source
class ShortcutForm extends ContentEntityForm {
/**
* The entity being used by this form.
*
* @var \Drupal\shortcut\ShortcutInterface
*/
protected $entity;
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$form['#attached']['library'][] = 'core/drupal.form';
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$status = $entity->save();
$url = $entity->getUrl();
// There's an edge case where a user can have permission to
// 'link to any content', but has no right to access the linked page. So we
// check the access before showing the link.
if ($url->access()) {
$view_link = Link::fromTextAndUrl($entity->getTitle(), $url)
->toString();
}
else {
$view_link = $entity->getTitle();
}
if ($status == SAVED_UPDATED) {
$message = $this->t('The shortcut %link has been updated.', [
'%link' => $view_link,
]);
}
else {
$message = $this->t('Added a shortcut for %title.', [
'%title' => $view_link,
]);
}
$this->messenger()
->addStatus($message);
$form_state->setRedirect('entity.shortcut_set.customize_form', [
'shortcut_set' => $entity->bundle(),
]);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.