You are here

private function LayouterForm::imageContentHandler in Layouter - WYSIWYG layout templates 8

Returns the form item with actual settings, to upload image.

Parameters

string $name: Field name.

array $params: Additional parameters for field from layouter template.

Return value

array Renderable array for file field.

1 call to LayouterForm::imageContentHandler()
LayouterForm::buildLayouterFields in src/Form/LayouterForm.php
Sets up and builds fields from selected layouter template.

File

src/Form/LayouterForm.php, line 251

Class

LayouterForm
Provides multistep ajax form for an layout choice.

Namespace

Drupal\layouter\Form

Code

private function imageContentHandler($name, array $params) {
  $fieldset_name = 'image_' . $name;

  // Fieldset for image fields.
  $result['#type'] = 'fieldset';
  $result['#title'] = $params['title'];

  // Prepare managed_file field.
  $allowed_extensions = [
    'png gif jpeg jpg',
  ];
  $max_upload_size_mb = (int) ini_get('upload_max_filesize');
  $max_upload_size = [
    $max_upload_size_mb * 1024 * 1024,
  ];
  $image_field_description = $this
    ->t('Files must be less than @size.', [
    '@size' => format_size($max_upload_size[0]),
  ]);
  $image_field_description .= '<br />' . $this
    ->t('Allowed file types: @extensions.', [
    '@extensions' => $allowed_extensions[0],
  ]);
  if (!empty($params['description'])) {
    $image_field_description .= '<br />' . $params['description'];
  }
  $location_scheme = \Drupal::config('layouter.settings')
    ->get('uri_scheme');

  // Add managed_file field and textfield for image alternative text.
  $result[$name] = [
    '#type' => 'managed_file',
    '#title' => $this
      ->t('Image'),
    '#field_name' => 'layouter_image',
    '#description' => $image_field_description,
    '#required' => 1,
    '#upload_location' => $location_scheme . '://layouter_images',
    '#upload_validators' => [
      'file_validate_extensions' => $allowed_extensions,
      'file_validate_size' => [
        $max_upload_size,
      ],
    ],
  ];
  $result[$name . '_alt'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Alternative text'),
  ];

  // Prepare list field with allowed image styles.
  if (\Drupal::currentUser()
    ->hasPermission('administer image styles')) {
    $url = Url::fromRoute('entity.image_style.collection')
      ->getInternalPath();
    $description = $this
      ->t('You can also') . ' <a href="/' . $url . '" target="_blank">' . $this
      ->t('add your own image style') . '</a> ' . $this
      ->t('if you need to.');
    $admin_image_style_description = $description;
  }
  else {
    $admin_image_style_description = '';
  }
  $image_styles = \Drupal::config('layouter.settings')
    ->get('image_styles');
  $image_styles_options['none'] = 'none';
  foreach ($image_styles as $k => $v) {
    if ($v != '0') {
      $image_styles_options[$k] = $v;
    }
  }

  // Add image style field to result.
  $result[$name . '_style'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Image style'),
    '#required' => TRUE,
    '#options' => $image_styles_options,
    '#description' => $admin_image_style_description,
  ];
  $fieldset[$fieldset_name] = $result;
  return $fieldset;
}