cloud-8.x-2.0-beta1/src/Form/CloudContentForm.php

src/Form/CloudContentForm.php
<?php

namespace Drupal\cloud\Form;

use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Messenger\Messenger;
use Drupal\Component\Datetime\TimeInterface;

use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Form controller for the Cloud entity edit forms.
 *
 * @ingroup cloud
 */
class CloudContentForm extends ContentEntityForm {

  /**
   * {@inheritdoc}
   */
  protected $manager;

  /**
   * The Messenger service.
   *
   * @var \Drupal\Core\Messenger\Messenger
   */
  protected $messenger;

  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * {@inheritdoc}
   */
  public function __construct(EntityManagerInterface $manager,
                              EntityRepositoryInterface $entity_repository,
                              Messenger $messenger,
                              TimeInterface $time = NULL) {
    $this->manager = $manager;
    $this->entityRepository = $entity_repository;
    $this->messenger = $messenger;
    $this->time = $time ?: \Drupal::time();
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity.manager'),
      $container->get('entity.repository'),
      $container->get('messenger'),
      $container->has('datetime.time') ? $container->get('datetime.time') : NULL
    );
  }

  /**
   * Override actions()
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return array
   *   Form definition array.
   */
  public function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form, $form_state);
    $entity = $this->entity;
    foreach ($actions as $key => $action) {
      if (isset($actions[$key]['#url'])
      && method_exists($this->entity, 'cloud_context')) {
        $actions[$key]['#url']->setRouteParameter('cloud_context', $entity->getCloudContext());
      }
    }
    return $actions;
  }

  /**
   * Overrides \Drupal\Core\Entity\EntityFormController::submit().
   */
  public function submit(array $form, FormStateInterface $form_state) {
    // Build the entity object from the submitted values.
    $entity = parent::submit($form, $form_state);

    return $entity;
  }

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

    $entity = $this->entity;
    $status = $entity->save();

    $message = $this->t('The @label "%label" has been saved.', [
      '@label' => $entity->getEntityType()->getLabel(),
      '%label' => $entity->label(),
    ]);

    $this->messenger->addMessage($message, $status);
    return $status;
  }

  /**
   * Add the build array of fieldset others.
   *
   * @param array &$form
   *   The form array.
   * @param int $weight
   *   The weight of the fieldset.  The parameter's default value is 1
   *   to put the "Others" fieldset in between the main items and button(s)
   *   (e.g. "Save") if the parameter is omitted since 0 is the default value
   *   of the #weight attribute.
   * @param string $cloud_context
   *   The cloud context.
   */
  protected function addOthersFieldset(array &$form, $weight = 1, $cloud_context = '') {

    $form['others'] = [
      '#type'          => 'details',
      '#title'         => $this->t('Others'),
      '#open'          => FALSE,
      '#weight'       => $weight,
    ];

    $form['others']['cloud_context'] = [
      '#type'          => 'textfield',
      '#title'         => $this->t('Cloud Service Provider ID'),
      '#maxlength'     => 255,
      '#size'          => 60,
      '#default_value' => !$this->entity->isNew()
      ? $this->entity->getCloudContext()
      : $cloud_context,
      '#required'      => TRUE,
      '#disabled'      => TRUE,
    ];

    $form['others']['langcode'] = [
      '#title'         => t('Language'),
      '#type'          => 'language_select',
      '#default_value' => $this->entity->getUntranslated()->language()->getId(),
      '#languages'     => Language::STATE_ALL,
      '#attributes'    => ['readonly' => 'readonly'],
      '#disabled'      => FALSE,
    ];

    $form['others']['uid'] = $form['uid'];
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc