You are here

class DescriptionForm in Filebrowser 3.x

Same name and namespace in other branches
  1. 8.2 src/Form/DescriptionForm.php \Drupal\filebrowser\Form\DescriptionForm

Hierarchy

Expanded class hierarchy of DescriptionForm

File

src/Form/DescriptionForm.php, line 9

Namespace

Drupal\filebrowser\Form
View source
class DescriptionForm extends FormBase {

  /**
   * @var array
   * Array of ids of the entities to be edited
   */
  protected $e_ids;

  /**
   * @var array
   * Array of entities to be edited.
   */
  protected $entities;
  protected $nid;
  protected $queryFid;

  /**
   * @var \Drupal\filebrowser\Services\Common
   */
  protected $common;

  /**
   * @var \Drupal\filebrowser\Services\FilebrowserStorage
   */
  protected $storage;

  /**
   * @inheritdoc
   */
  public function getFormId() {
    return 'filebrowser_form_edit_file_description';
  }

  /**
   * @param int $nid
   * @param int $query_fid
   * @param array $fids
   * @param $form
   * @param $form_state
   * @param $ajax
   * @return array
   */
  public function buildForm(array $form, FormStateInterface $form_state, $nid = null, $query_fid = null, $fids = null, $ajax = null) {
    $fid_array = explode(',', $fids);
    $this->nid = $nid;
    $this->queryFid = $query_fid;
    $this->common = \Drupal::service('filebrowser.common');

    // we need to load the fileData to retrieve the filename
    $this->storage = \Drupal::service('filebrowser.storage');
    $file_data = $this->storage
      ->nodeContentLoadMultiple($fid_array);

    // Load the description-metadata
    $ids = \Drupal::entityQuery('filebrowser_metadata_entity')
      ->condition('fid', $fid_array, "IN")
      ->condition('name', 'description')
      ->execute();
    $this->entities = \Drupal::entityTypeManager()
      ->getStorage('filebrowser_metadata_entity')
      ->loadMultiple($ids);
    $descriptions = [];
    foreach ($this->entities as $entity) {
      $descriptions[$entity->fid->value] = unserialize($entity->content->value)['title'];
    }

    // if this form is opened by ajax add a close link.
    if ($ajax) {
      $form['#attributes'] = [
        'class' => [
          'form-in-slide-down',
        ],
      ];
      $form['close'] = $this->common
        ->closeButtonMarkup();
    }
    $form['items'] = [
      '#title' => $this
        ->t('Edit description'),
      '#type' => 'fieldset',
      '#tree' => true,
    ];
    foreach ($descriptions as $key => $description) {
      $properties = unserialize($file_data[$key]['file_data']);

      //debug($properties);
      $form['items'][$key] = [
        '#type' => 'textarea',
        '#title' => $properties->filename,
        '#rows' => 3,
        '#default_value' => $description,
      ];
    }
    $form['cancel'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Cancel'),
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save'),
    ];
    return $form;
  }

  /**
   * @inheritdoc
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    /** @var FilebrowserMetadataEntity $entity */
    $values = $form_state
      ->getValue('items');
    foreach ($this->entities as $entity) {
      $content = unserialize($entity->content->value);
      $fid = $entity->fid->value;
      $content['title'] = $values[$fid];
      $entity
        ->setContent(serialize($content));
      $entity
        ->save();
    }
    if ($this->nid) {
      $route = $this->common
        ->redirectRoute($this->queryFid, $this->nid);
      $form_state
        ->setRedirect($route['name'], $route['node'], $route['query']);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
DescriptionForm::$common protected property
DescriptionForm::$entities protected property Array of entities to be edited.
DescriptionForm::$e_ids protected property Array of ids of the entities to be edited
DescriptionForm::$nid protected property
DescriptionForm::$queryFid protected property
DescriptionForm::$storage protected property
DescriptionForm::buildForm public function _state Overrides FormInterface::buildForm
DescriptionForm::getFormId public function @inheritdoc Overrides FormInterface::getFormId
DescriptionForm::submitForm public function @inheritdoc Overrides FormInterface::submitForm
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 105
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.