public function PhotosImageEditForm::buildForm in Album Photos 8.4
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- src/
Form/ PhotosImageEditForm.php, line 93
Class
- PhotosImageEditForm
- Defines a form to edit images.
Namespace
Drupal\photos\FormCode
public function buildForm(array $form, FormStateInterface $form_state, $image = NULL, $type = 'album') {
$user = $this
->currentUser();
// Get node object.
$node = $this->entityTypeManager
->getStorage('node')
->load($image->pid);
$nid = $node
->id();
$cover = isset($node->album['cover']) ? $node->album['cover'] : [];
$image->info = [
'cover' => $cover,
'pid' => $node
->id(),
'title' => $node
->getTitle(),
'uid' => $node
->getOwnerId(),
];
if ($node
->getType() == 'photos') {
// Album.
$album_update = '';
if ($image && $user
->id() != $image->info['uid']) {
$title = isset($image->info['title']) ? $image->info['title'] : '';
$album_update = [
$nid,
$image->info['title'],
];
}
else {
$album_update = '';
}
$uid = $image ? $image->uid : $user
->id();
$album_pid = PhotosAlbum::userAlbumOptions($uid, $album_update);
$del_label = $this
->t('Delete');
if (isset($node->album) && isset($node->album['cover']['fid'])) {
$form['cover_fid'] = [
'#type' => 'hidden',
'#default_value' => $node->album['cover']['fid'],
];
}
$form['oldpid'] = [
'#type' => 'hidden',
'#default_value' => $nid,
];
}
$form['nid'] = [
'#type' => 'hidden',
'#default_value' => $nid,
];
$form['type'] = [
'#type' => 'hidden',
'#value' => $type,
];
$form['fid'] = [
'#type' => 'hidden',
'#value' => $image->fid,
];
$form['del'] = [
'#title' => $del_label,
'#type' => 'checkbox',
];
$image->user = $this->entityTypeManager
->getStorage('user')
->load($image->uid);
$image->href = 'photos/image/' . $image->fid;
$item = [];
$title = $image->title;
$image_sizes = $this
->config('photos.settings')
->get('photos_size');
$style_name = key($image_sizes);
$image_view = [
'#theme' => 'image_style',
'#style_name' => $style_name,
'#uri' => $image->uri,
'#alt' => $title,
'#title' => $title,
];
$item[] = Link::fromTextAndUrl($image_view, Url::fromUri('base:' . $image->href), [
'html' => TRUE,
'attributes' => [
'title' => $title,
],
]);
if ($type == 'album' && (!isset($cover['fid']) || isset($cover['fid']) && $image->fid != $cover['fid'])) {
// Set cover link.
$cover_url = Url::fromRoute('photos.album.update.cover', [
'node' => $image->pid,
'file' => $image->fid,
]);
$item[] = Link::fromTextAndUrl($this
->t('Set to Cover'), $cover_url);
}
if (isset($image->filesize)) {
// @todo update to use MB?
$size = round($image->filesize / 1024);
$item[] = $this
->t('Filesize: @size KB', [
'@size' => number_format($size),
]);
}
if (isset($image->count)) {
$item[] = $this
->t('Visits: @count', [
'@count' => $image->count,
]);
}
if (isset($image->comcount)) {
$item[] = $this
->t('Comments: @count', [
'@count' => $image->comcount,
]);
}
$form['title'] = [
'#title' => $this
->t('Image title'),
'#type' => 'textfield',
'#default_value' => isset($image->title) ? $image->title : '',
'#required' => FALSE,
];
$form['path'] = [
'#theme' => 'item_list',
'#items' => $item,
];
// Check for cropper module and add image_crop field.
if ($this->moduleHandler
->moduleExists('image_widget_crop') && ($crop_config = $this
->config('image_widget_crop.settings'))) {
if ($crop_config
->get('settings.crop_list')) {
$file = $this->entityTypeManager
->getStorage('file')
->load($image->fid);
// @todo move to form alter along with submit handler.
$form['image_crop'] = [
'#type' => 'image_crop',
'#file' => $file,
'#crop_type_list' => $crop_config
->get('settings.crop_list'),
'#crop_preview_image_style' => $crop_config
->get('settings.crop_preview_image_style'),
'#show_default_crop' => $crop_config
->get('settings.show_default_crop'),
'#warn_mupltiple_usages' => $crop_config
->get('settings.warn_mupltiple_usages'),
];
}
}
$form['des'] = [
'#title' => $this
->t('Image description'),
'#type' => 'textarea',
'#default_value' => isset($image->des) ? $image->des : '',
'#cols' => 40,
'#rows' => 4,
];
$form['wid'] = [
'#title' => $this
->t('Weight'),
'#type' => 'textfield',
'#size' => 5,
'#default_value' => isset($image->wid) ? $image->wid : NULL,
];
$form['filepath'] = [
'#type' => 'value',
'#value' => $image->uri,
];
if ($type == 'album') {
$username = [
'#theme' => 'username',
'#account' => $image->user,
];
$upload_info = $this
->t('Uploaded on @time by @name', [
'@name' => $this->renderer
->renderPlain($username),
'@time' => $this->dateFormatter
->format($image->created, 'short'),
]);
$form['pid'] = [
'#title' => $this
->t('Move to album'),
'#type' => 'select',
'#options' => $album_pid,
'#default_value' => $image->pid,
'#required' => TRUE,
];
}
else {
$upload_info = $this
->t('Uploaded by @name on @time to @title', [
'@name' => [
'#theme' => 'username',
'#account' => $image->user,
],
'@time' => $this->dateFormatter
->format($image->created, 'short'),
'@title' => Link::fromTextAndUrl($image->album_title, Url::fromUri('base:node/' . $image->pid)),
]);
}
$form['time']['#markup'] = $upload_info;
$form['uid'] = [
'#type' => 'hidden',
'#default_value' => $image->uid,
];
$form['oldtitle'] = [
'#type' => 'hidden',
'#default_value' => $image->title,
];
if (!empty($image)) {
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Confirm changes'),
];
}
return $form;
}