MediaForm.php in Drupal 8
File
core/modules/media/src/MediaForm.php
View source
<?php
namespace Drupal\media;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
class MediaForm extends ContentEntityForm {
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$media_type = $this->entity->bundle->entity;
if ($this->operation === 'edit') {
$form['#title'] = $this
->t('Edit %type_label @label', [
'%type_label' => $media_type
->label(),
'@label' => $this->entity
->label(),
]);
}
if (isset($form['uid']) || isset($form['created'])) {
$form['author'] = [
'#type' => 'details',
'#title' => $this
->t('Authoring information'),
'#group' => 'advanced',
'#attributes' => [
'class' => [
'media-form-author',
],
],
'#weight' => 90,
'#optional' => TRUE,
];
}
if (isset($form['uid'])) {
$form['uid']['#group'] = 'author';
}
if (isset($form['created'])) {
$form['created']['#group'] = 'author';
}
$form['#attached']['library'][] = 'media/form';
return $form;
}
public function save(array $form, FormStateInterface $form_state) {
$saved = parent::save($form, $form_state);
$context = [
'@type' => $this->entity
->bundle(),
'%label' => $this->entity
->label(),
'link' => $this->entity
->toLink($this
->t('View'))
->toString(),
];
$logger = $this
->logger('media');
$t_args = [
'@type' => $this->entity->bundle->entity
->label(),
'%label' => $this->entity
->toLink($this->entity
->label())
->toString(),
];
if ($saved === SAVED_NEW) {
$logger
->notice('@type: added %label.', $context);
$this
->messenger()
->addStatus($this
->t('@type %label has been created.', $t_args));
}
else {
$logger
->notice('@type: updated %label.', $context);
$this
->messenger()
->addStatus($this
->t('@type %label has been updated.', $t_args));
}
if ($this
->currentUser()
->hasPermission('access media overview')) {
$form_state
->setRedirectUrl($this->entity
->toUrl('collection'));
}
else {
$form_state
->setRedirectUrl($this->entity
->toUrl());
}
return $saved;
}
}
Classes
Name |
Description |
MediaForm |
Form controller for the media edit forms. |