Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
novicell/custom_forms / src / Form / CustomFormForm.php
Size: Mime:
<?php

namespace Drupal\custom_forms\Form;

use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;

/**
 * Form controller for the custom form entity edit forms.
 *
 * This is the backend add/edit form.
 */
class CustomFormForm extends ContentEntityForm {

  /** @var \Drupal\Core\Session\AccountProxyInterface */
  protected $currentUser;

  /**
   * @inheritDoc
   */
  public function __construct(\Drupal\Core\Entity\EntityRepositoryInterface $entity_repository, \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, \Drupal\Component\Datetime\TimeInterface $time = NULL) {
    parent::__construct($entity_repository, $entity_type_bundle_info, $time);
    $this->currentUser = \Drupal::currentUser();
  }

  /**
   * @inheritDoc
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);

    /** @var \Drupal\custom_forms\CustomFormInterface $entity */
    $entity = $this->entity;
    /** @var \Drupal\Core\Datetime\DateFormatter $date_formatter */
    $date_formatter = \Drupal::service('date.formatter');
    $form['#theme'] = 'custom_form_form';
    $form['advanced']['#attributes']['class'][] = 'entity-meta';
    $form['advanced']['#type'] = 'container';

    $form['meta'] = [
      '#type' => 'container',
      '#group' => 'advanced',
      '#weight' => -10,
      '#title' => $this->t('Status'),
      '#attributes' => ['class' => ['entity-meta__header']],
      '#tree' => TRUE,
      '#access' => TRUE,
    ];
    $form['meta']['status'] = [
      '#type' => 'item',
      '#markup' => $entity->isPublished() ? $this->t('Published') : $this->t('Not published'),
      '#access' => !$entity->isNew(),
      '#wrapper_attributes' => ['class' => ['entity-meta__title']],
    ];
    $form['meta']['changed'] = [
      '#type' => 'item',
      '#title' => $this->t('Last saved'),
      '#markup' => !$entity->isNew() ? $date_formatter->format($entity->getChangedTime(), 'short') : $this->t('Not saved yet'),
      '#wrapper_attributes' => ['class' => ['entity-meta__last-saved', 'container-inline']],
    ];
    $form['meta']['author'] = [
      '#type' => 'item',
      '#title' => $this->t('Author'),
      '#markup' => $entity->getOwner()->getAccountName(),
      '#wrapper_attributes' => ['class' => ['entity-meta__author', 'container-inline']],
    ];

    $form['status']['#group'] = 'footer';

    // Custom form author information for administrators.
    $form['author'] = [
      '#type' => 'details',
      '#title' => t('Authoring information'),
      '#group' => 'advanced',
      '#attributes' => [
        'class' => ['custom-form-author'],
      ],
      '#attached' => [
        'library' => ['node/drupal.node'],
      ],
      '#weight' => 90,
      '#optional' => TRUE,
    ];

    if (isset($form['uid'])) {
      $form['uid']['#group'] = 'author';
    }

    if (isset($form['created'])) {
      $form['created']['#group'] = 'author';
    }

    if (isset($form['revision_log_message'])) {
      $form['revision_log_message']['#group'] = 'meta';
    }

    if (isset($form['revision_information'])) {
      $form['revision_information']['#type'] = 'container';
      $form['revision_information']['#group'] = 'meta';
    }

    $form['#attached']['library'][] = 'node/form';
    $form['#attached']['library'][] = 'claro/node-form';

    return $form;
  }


  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {

    $entity = $this->getEntity();
    $result = $entity->save();
    $link = $entity->toLink($this->t('View'))->toRenderable();

    $message_arguments = ['%label' => $this->entity->label()];
    $logger_arguments = $message_arguments + ['link' => \Drupal::service('renderer')->render($link)];

    if ($result == SAVED_NEW) {
      $this->messenger()->addMessage($this->t('New custom form %label has been created.', $message_arguments));
      $this->logger('Custom Forms')->notice('Created new custom form %label', $logger_arguments);
    }
    else {
      $this->messenger()->addMessage($this->t('The custom form %label has been updated.', $message_arguments));
      $this->logger('Custom Forms')->notice('Created new custom form %label.', $logger_arguments);
    }

    $form_state->setRedirect('entity.custom_form.edit_form', ['custom_form' => $entity->id()]);
  }

}