public function PhotosUploadForm::buildForm in Album Photos 6.0.x
Same name and namespace in other branches
- 8.5 src/Form/PhotosUploadForm.php \Drupal\photos\Form\PhotosUploadForm::buildForm()
- 8.4 src/Form/PhotosUploadForm.php \Drupal\photos\Form\PhotosUploadForm::buildForm()
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/ PhotosUploadForm.php, line 182
Class
- PhotosUploadForm
- Defines a form to upload photos to this site.
Namespace
Drupal\photos\FormCode
public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL) {
$config = $this
->config('photos.settings');
$nid = $node
->id();
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['new'] = [
'#title' => $this
->t('Image upload'),
'#weight' => -4,
'#type' => 'details',
'#open' => TRUE,
];
$allow_zip = $config
->get('photos_upzip') ? ' zip' : '';
// Check if plubload is installed.
if ($config
->get('photos_plupload_status')) {
$form['new']['plupload'] = [
'#type' => 'plupload',
'#title' => $this
->t('Upload photos'),
'#description' => $this
->t('Upload multiple images.'),
'#autoupload' => TRUE,
'#submit_element' => '#edit-submit',
'#upload_validators' => [
'file_validate_extensions' => [
'jpg jpeg gif png' . $allow_zip,
],
],
'#plupload_settings' => [
'chunk_size' => '1mb',
],
];
}
else {
// Manual upload form.
$form['new']['#description'] = $this
->t('Allowed types: jpg gif png jpeg@zip', [
'@zip' => $allow_zip,
]);
$album_photo_limit = $config
->get('album_photo_limit');
$classic_field_count = $config
->get('photos_num');
if ($album_photo_limit && $classic_field_count > $album_photo_limit) {
$classic_field_count = $album_photo_limit;
}
for ($i = 0; $i < $classic_field_count; ++$i) {
$form['new']['images_' . $i] = [
'#type' => 'file',
];
$form['new']['title_' . $i] = [
'#type' => 'textfield',
'#title' => $this
->t('Image title'),
];
$form['new']['des_' . $i] = [
'#type' => 'textarea',
'#title' => $this
->t('Image description'),
'#cols' => 40,
'#rows' => 3,
];
}
}
if ($this->moduleHandler
->moduleExists('media_library_form_element')) {
// Check photos default multi-upload field.
$uploadField = $this
->config('photos.settings')
->get('multi_upload_default_field');
$uploadFieldParts = explode(':', $uploadField);
$field = isset($uploadFieldParts[0]) ? $uploadFieldParts[0] : 'field_image';
$allBundleFields = $this->entityFieldManager
->getFieldDefinitions('photos_image', 'photos_image');
if (isset($allBundleFields[$field])) {
$fieldType = $allBundleFields[$field]
->getType();
// Check if media field.
if ($fieldType == 'entity_reference') {
$mediaField = isset($uploadFieldParts[1]) ? $uploadFieldParts[1] : '';
$mediaBundle = isset($uploadFieldParts[2]) ? $uploadFieldParts[2] : '';
if ($mediaField && $mediaBundle) {
$form['new']['media_images'] = [
'#type' => 'media_library',
'#allowed_bundles' => [
$mediaBundle,
],
'#title' => $this
->t('Select media images'),
'#default_value' => NULL,
'#description' => $this
->t('Select media images to add to this album.'),
'#cardinality' => -1,
];
}
}
}
}
// @todo album_id is redundant unless albums become own entity.
// - maybe make album_id serial and add nid... or entity_id.
$form['new']['album_id'] = [
'#type' => 'value',
'#value' => $nid,
];
$form['new']['nid'] = [
'#type' => 'value',
'#value' => $nid,
];
$form['new']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Confirm upload'),
'#weight' => 10,
];
return $form;
}