You are here

public function PhotosImageEditForm::form in Album Photos 8.5

Same name and namespace in other branches
  1. 6.0.x src/Form/PhotosImageEditForm.php \Drupal\photos\Form\PhotosImageEditForm::form()

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/PhotosImageEditForm.php, line 113

Class

PhotosImageEditForm
Defines a form to edit images.

Namespace

Drupal\photos\Form

Code

public function form(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\photos\PhotosImageInterface $photosImage */
  $photosImage = $this->entity;

  // @todo phase out type (no more sub-albums...).
  $type = 'album';
  $user = $this
    ->currentUser();
  $form['#title'] = $this
    ->t('Edit @title', [
    '@title' => $photosImage
      ->getTitle(),
  ]);

  // Changed must be sent to the client, for later overwrite error checking.
  $form['changed'] = [
    '#type' => 'hidden',
    '#default_value' => $photosImage
      ->getChangedTime(),
  ];

  // Get node object.
  $node = $this->entityTypeManager
    ->getStorage('node')
    ->load($photosImage
    ->getAlbumId());
  $nid = $node
    ->id();
  $cover = isset($node->album['cover']) ? $node->album['cover'] : [];
  $photosImage->info = [
    'cover' => $cover,
    'pid' => $node
      ->id(),
    'title' => $node
      ->getTitle(),
    'uid' => $node
      ->getOwnerId(),
  ];

  // @todo build imageView?
  $imageView = [];
  $imageView['photos_image'] = $photosImage;

  // Album.
  $album_update = '';
  if ($photosImage && $user
    ->id() != $photosImage->info['uid']) {
    $title = isset($photosImage->info['title']) ? $photosImage->info['title'] : '';
    $album_update = [
      $nid,
      $photosImage->info['title'],
    ];
  }
  $uid = $photosImage ? $photosImage
    ->getOwnerId() : $user
    ->id();
  $form['old_uid'] = [
    '#type' => 'hidden',
    '#default_value' => $uid,
  ];

  // $albumOptions = PhotosAlbum::userAlbumOptions($uid, $album_update);
  if (isset($node->album) && isset($node->album['cover']['id'])) {
    $form['cover_id'] = [
      '#type' => 'hidden',
      '#default_value' => $node->album['cover']['id'],
    ];
  }
  $form['old_album_id'] = [
    '#type' => 'hidden',
    '#default_value' => $nid,
  ];
  $form['nid'] = [
    '#type' => 'hidden',
    '#default_value' => $nid,
  ];

  // $form['type'] = ['#type' => 'hidden', '#value' => $type];
  $account = $this->entityTypeManager
    ->getStorage('user')
    ->load($photosImage
    ->getOwnerId());
  $imageView['href'] = 'photos/' . $photosImage
    ->getAlbumId() . '/' . $photosImage
    ->id();
  $item = [];
  $title = $photosImage
    ->getTitle();
  $image_sizes = $this
    ->config('photos.settings')
    ->get('photos_size');
  $style_name = key($image_sizes);
  if ($type == 'album' && (!isset($cover['id']) || isset($cover['id']) && $photosImage
    ->id() != $cover['id'])) {

    // Set cover link.
    $cover_url = Url::fromRoute('photos.album.update.cover', [
      'node' => $photosImage
        ->getAlbumId(),
      'photos_image' => $photosImage
        ->id(),
    ], [
      'attributes' => [
        'target' => '_blank',
      ],
    ]);
    $item[] = Link::fromTextAndUrl($this
      ->t('Set to Cover'), $cover_url);
  }

  // @todo counts.
  $form['cover_items'] = [
    '#theme' => 'item_list',
    '#items' => $item,
  ];
  $username = [
    '#theme' => 'username',
    '#account' => $account,
  ];
  $upload_info = $this
    ->t('Uploaded on @time by @name', [
    '@name' => $this->renderer
      ->renderPlain($username),
    '@time' => $this->dateFormatter
      ->format($photosImage
      ->getCreatedTime(), 'short'),
  ]);

  // @todo test moving image with album reference field.
  $form['time']['#markup'] = $upload_info;
  $form['oldtitle'] = [
    '#type' => 'hidden',
    '#default_value' => $photosImage
      ->getTitle(),
  ];
  return parent::form($form, $form_state);
}