You are here

public function PhotosAdminSettingsForm::buildForm in Album Photos 8.4

Same name and namespace in other branches
  1. 8.5 src/Form/PhotosAdminSettingsForm.php \Drupal\photos\Form\PhotosAdminSettingsForm::buildForm()
  2. 6.0.x src/Form/PhotosAdminSettingsForm.php \Drupal\photos\Form\PhotosAdminSettingsForm::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 ConfigFormBase::buildForm

File

src/Form/PhotosAdminSettingsForm.php, line 53

Class

PhotosAdminSettingsForm
Defines a form to configure maintenance settings for this site.

Namespace

Drupal\photos\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);

  // Get variables for default values.
  $config = $this
    ->config('photos.settings');

  // Load custom admin css and js library.
  $form['#attached']['library'] = [
    'photos/photos.admin',
  ];
  $form['basic'] = [
    '#title' => $this
      ->t('Basic settings'),
    '#weight' => -5,
    '#type' => 'details',
  ];

  // Photos access integration settings.
  $module_photos_access_exists = $this->moduleHandler
    ->moduleExists('photos_access');
  $url = Url::fromRoute('system.modules_list', [], [
    'fragment' => 'module-photos-access',
  ]);
  $link = Link::fromTextAndUrl('photos_access', $url)
    ->toString();
  $warning_msg = '';

  // Set warning if private file path is not set.
  if (!PrivateStream::basePath() && $config
    ->get('photos_access_photos')) {
    $warning_msg = $this
      ->t('Warning: image files can still be accessed by visiting the direct URL.
        For better security, ask your website admin to setup a private file path.');
  }
  $form['basic']['photos_access_photos'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Privacy settings'),
    '#default_value' => $config
      ->get('photos_access_photos') ?: 0,
    '#description' => $module_photos_access_exists ? $warning_msg : $this
      ->t('Enable the @link module.', [
      '@link' => $link,
    ]),
    '#options' => [
      $this
        ->t('Disabled'),
      $this
        ->t('Enabled'),
    ],
    '#required' => TRUE,
    '#disabled' => $module_photos_access_exists ? FALSE : TRUE,
  ];

  // Classic upload form settings.
  $num_options = [
    1 => 1,
    2 => 2,
    3 => 3,
    4 => 4,
    5 => 5,
    6 => 6,
    7 => 7,
    8 => 8,
    9 => 9,
    10 => 10,
  ];
  $form['basic']['photos_num'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Classic form'),
    '#default_value' => $config
      ->get('photos_num'),
    '#required' => TRUE,
    '#options' => $num_options,
    '#description' => $this
      ->t('Maximum number of upload fields on the classic upload form.'),
  ];

  // Plupload integration settings.
  $module_plupload_exists = $this->moduleHandler
    ->moduleExists('plupload');
  if ($module_plupload_exists) {
    $form['basic']['photos_plupload_status'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use Plupoad for file uploads'),
      '#default_value' => $config
        ->get('photos_plupload_status'),
    ];
  }
  else {
    $config
      ->set('photos_plupload_status', 0)
      ->save();
    $form['basic']['photos_plupload_status'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use Plupoad for file uploads'),
      '#disabled' => TRUE,
      '#description' => $this
        ->t('To enable multiuploads and drag&drop upload features, download and install the @link module', [
        '@link' => Link::fromTextAndUrl($this
          ->t('Plupload integration'), Url::fromUri('http://drupal.org/project/plupload'))
          ->toString(),
      ]),
    ];
  }

  // Check if colorbox is enabled.
  $colorbox = FALSE;
  if ($this->moduleHandler
    ->moduleExists('colorbox')) {
    $colorbox = TRUE;
  }

  // Photos module settings.
  // @todo token integration.
  $form['basic']['photos_path'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Path'),
    '#default_value' => $config
      ->get('photos_path'),
    '#description' => $this
      ->t('The path where the files will be saved relative to the files folder.
        Available variables: %uid, %username, %Y, %m, %d.'),
    '#size' => '40',
    '#required' => TRUE,
  ];
  $form['basic']['photos_size_max'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Maximum image resolution'),
    '#default_value' => $config
      ->get('photos_size_max'),
    '#description' => $this
      ->t('The maximum image resolution example: 800x600. If an image toolkit is available the image will be scaled
        to fit within the desired maximum dimensions. Make sure this size is larger than any image styles used.
        Leave blank for no restrictions.'),
    '#size' => '40',
  ];
  $form['basic']['photos_comment'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Comment setting'),
    '#default_value' => $config
      ->get('photos_comment') ?: 0,
    '#description' => $this
      ->t('Enable to comment on single photo. You must also open comments for content type / node.'),
    '#required' => TRUE,
    '#options' => [
      $this
        ->t('Disabled'),
      $this
        ->t('Enabled'),
    ],
  ];
  $form['basic']['photos_upzip'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Allow zip upload'),
    '#default_value' => $config
      ->get('photos_upzip') ?: 0,
    '#description' => $this
      ->t('Will be allowed to upload images compressed into a zip folder.'),
    '#options' => [
      $this
        ->t('Disabled'),
      $this
        ->t('Enabled'),
    ],
  ];

  // @todo look into transliteration integration D8 core.
  $form['basic']['photos_rname'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Rename image'),
    '#default_value' => $config
      ->get('photos_rname') ?: 0,
    '#description' => $this
      ->t('Rename uploaded image by random numbers, to solve problems with non-ASCII filenames such as Chinese.'),
    '#required' => TRUE,
    '#options' => [
      $this
        ->t('Disabled'),
      $this
        ->t('Enabled'),
    ],
  ];
  $form['basic']['num'] = [
    '#title' => $this
      ->t('Number of albums'),
    '#weight' => 10,
    '#type' => 'details',
    '#description' => $this
      ->t('The number of albums a user allowed to create. Administrater is not limited.'),
    '#tree' => TRUE,
  ];
  $roles = user_roles(TRUE);
  foreach ($roles as $key => $role) {
    $form['basic']['num']['photos_pnum_' . $key] = [
      '#type' => 'number',
      '#title' => $role
        ->label(),
      '#required' => TRUE,
      '#default_value' => $config
        ->get('photos_pnum_' . $key) ? $config
        ->get('photos_pnum_' . $key) : 20,
      '#min' => 1,
      '#step' => 1,
      '#prefix' => '<div class="photos-admin-inline">',
      '#suffix' => '</div>',
      '#size' => 10,
    ];
  }

  // Thumb settings.
  if ($size = PhotosImage::sizeInfo()) {
    $num = $size['count'] + 3;
    $sizes = [];
    foreach ($size['size'] as $style => $label) {
      $sizes[] = [
        'style' => $style,
        'label' => $label,
      ];
    }
    $size['size'] = $sizes;
  }
  else {

    // @todo remove else or use $size_options?
    $num = 3;
    $size['size'] = [
      [
        'style' => 'medium',
        'label' => 'Medium',
      ],
      [
        'style' => 'large',
        'label' => 'Large',
      ],
      [
        'style' => 'thumbnail',
        'label' => 'Thumbnail',
      ],
    ];
  }
  $form['photos_thumb_count'] = [
    '#type' => 'hidden',
    '#default_value' => $num,
  ];
  $form['thumb'] = [
    '#title' => $this
      ->t('Image sizes'),
    '#weight' => -4,
    '#type' => 'details',
    '#description' => $this
      ->t('Default image sizes. Note: if an image style is deleted after it has been in use for some
        time that may result in broken external image links.'),
  ];
  $thumb_options = image_style_options();
  if (empty($thumb_options)) {
    $image_style_link = Link::fromTextAndUrl($this
      ->t('add image styles'), Url::fromRoute('entity.image_style.collection'))
      ->toString();
    $form['thumb']['image_style'] = [
      '#markup' => '<p>One or more image styles required: ' . $image_style_link . '.</p>',
    ];
  }
  else {
    $form['thumb']['photos_pager_imagesize'] = [
      '#type' => 'select',
      '#title' => 'Pager size',
      '#default_value' => $config
        ->get('photos_pager_imagesize'),
      '#description' => $this
        ->t('Default pager block image style.'),
      '#options' => $thumb_options,
      '#required' => TRUE,
    ];
    $form['thumb']['photos_cover_imagesize'] = [
      '#type' => 'select',
      '#title' => 'Cover size',
      '#default_value' => $config
        ->get('photos_cover_imagesize'),
      '#description' => $this
        ->t('Default album cover image style.'),
      '#options' => $thumb_options,
      '#required' => TRUE,
    ];
    $form['thumb']['photos_name_0'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Name'),
      '#default_value' => isset($size['size'][0]['label']) ? $size['size'][0]['label'] : NULL,
      '#size' => '10',
      '#required' => TRUE,
      '#prefix' => '<div class="photos-admin-inline">',
    ];
    $form['thumb']['photos_size_0'] = [
      '#type' => 'select',
      '#title' => 'Thumb size',
      '#default_value' => isset($size['size'][0]['style']) ? $size['size'][0]['style'] : NULL,
      '#options' => $thumb_options,
      '#required' => TRUE,
      '#suffix' => '</div>',
    ];
    $empty_option = [
      '' => '',
    ];
    $thumb_options = $empty_option + $thumb_options;
    $form['thumb']['additional_sizes'] = [
      '#markup' => '<p>Additional image sizes ' . Link::fromTextAndUrl($this
        ->t('add more image styles'), Url::fromRoute('entity.image_style.collection'))
        ->toString() . '.</p>',
    ];
    $additional_sizes = 0;
    for ($i = 1; $i < $num; $i++) {
      $form['thumb']['photos_name_' . $i] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Name'),
        '#default_value' => isset($size['size'][$i]['label']) ? $size['size'][$i]['label'] : NULL,
        '#size' => '10',
        '#prefix' => '<div class="photos-admin-inline">',
      ];
      $form['thumb']['photos_size_' . $i] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Size'),
        '#default_value' => isset($size['size'][$i]['style']) ? $size['size'][$i]['style'] : NULL,
        '#options' => $thumb_options,
        '#suffix' => '</div>',
      ];
      $additional_sizes = $i;
    }
    $form['thumb']['photos_additional_sizes'] = [
      '#type' => 'hidden',
      '#value' => $additional_sizes,
    ];
  }

  // End thumb settings.
  // Display settings.
  $form['display'] = [
    '#title' => $this
      ->t('Display Settings'),
    '#type' => 'details',
  ];
  $form['display']['global'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Global Settings'),
    '#description' => $this
      ->t('Albums basic display settings'),
  ];
  $form['display']['page'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Page Settings'),
    '#description' => $this
      ->t('Page (e.g: node/[nid]) display settings'),
    '#prefix' => '<div id="photos-form-page">',
    '#suffix' => '</div>',
  ];
  $form['display']['teaser'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Teaser Settings'),
    '#description' => $this
      ->t('Teaser display settings'),
    '#prefix' => '<div id="photos-form-teaser">',
    '#suffix' => '</div>',
  ];
  $form['display']['global']['photos_album_display_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Album display'),
    '#required' => TRUE,
    '#default_value' => $config
      ->get('photos_album_display_type') ?: 'list',
    '#options' => [
      'list' => $this
        ->t('List'),
      'grid' => $this
        ->t('Grid'),
    ],
  ];
  $form['display']['global']['photos_display_viewpager'] = [
    '#type' => 'number',
    '#default_value' => $config
      ->get('photos_display_viewpager'),
    '#title' => $this
      ->t('How many images show in each page?'),
    '#required' => TRUE,
    '#min' => 1,
    '#step' => 1,
  ];
  $form['display']['global']['photos_album_column_count'] = [
    '#type' => 'number',
    '#default_value' => $config
      ->get('photos_album_column_count') ?: 2,
    '#title' => $this
      ->t('Number of columns'),
    '#description' => $this
      ->t('When using album grid view.'),
    '#required' => TRUE,
    '#min' => 1,
    '#step' => 1,
  ];
  $form['display']['global']['photos_display_imageorder'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Image display order'),
    '#required' => TRUE,
    '#default_value' => $config
      ->get('photos_display_imageorder'),
    '#options' => PhotosAlbum::orderLabels(),
  ];
  $list_imagesize = $config
    ->get('photos_display_list_imagesize');
  $view_imagesize = $config
    ->get('photos_display_view_imagesize');
  $size_options = PhotosImage::sizeOptions();
  $form['display']['global']['photos_display_list_imagesize'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Image display size (list)'),
    '#required' => TRUE,
    '#default_value' => $list_imagesize,
    '#description' => $this
      ->t('Displayed in the list (e.g: photos/album/[nid]) of image size.'),
    '#options' => $size_options,
  ];
  $form['display']['global']['photos_display_view_imagesize'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Image display size (page)'),
    '#required' => TRUE,
    '#default_value' => $view_imagesize,
    '#description' => $this
      ->t('Displayed in the page (e.g: photos/image/[fid]) of image size.'),
    '#options' => $size_options,
  ];
  $form['display']['global']['photos_display_user'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Allow users to modify this setting when they create a new album.'),
    '#default_value' => $config
      ->get('photos_display_user') ?: 0,
    '#options' => [
      $this
        ->t('Disabled'),
      $this
        ->t('Enabled'),
    ],
  ];
  if ($colorbox) {
    $form['display']['global']['photos_display_colorbox_max_height'] = [
      '#type' => 'number',
      '#default_value' => $config
        ->get('photos_display_colorbox_max_height') ?: 100,
      '#title' => $this
        ->t('Colorbox gallery maxHeight percentage.'),
      '#required' => TRUE,
      '#min' => 1,
      '#step' => 1,
    ];
    $form['display']['global']['photos_display_colorbox_max_width'] = [
      '#type' => 'number',
      '#default_value' => $config
        ->get('photos_display_colorbox_max_width') ?: 50,
      '#title' => $this
        ->t('Colorbox gallery maxWidth percentage.'),
      '#required' => TRUE,
      '#min' => 1,
      '#step' => 1,
    ];
  }
  $display_options = [
    $this
      ->t('Do not display'),
    $this
      ->t('Display cover'),
    $this
      ->t('Display thumbnails'),
  ];
  if ($colorbox) {
    $display_options[3] = $this
      ->t('Cover with colorbox gallery');
  }
  $form['display']['page']['photos_display_page_display'] = [
    '#type' => 'radios',
    '#default_value' => $config
      ->get('photos_display_page_display'),
    '#title' => $this
      ->t('Display setting'),
    '#required' => TRUE,
    '#options' => $display_options,
  ];
  $form['display']['page']['photos_display_full_viewnum'] = [
    '#type' => 'number',
    '#default_value' => $config
      ->get('photos_display_full_viewnum'),
    '#title' => $this
      ->t('Display quantity'),
    '#required' => TRUE,
    '#min' => 1,
    '#step' => 1,
    '#prefix' => '<div class="photos-form-count">',
  ];
  $form['display']['page']['photos_display_full_imagesize'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Image display size'),
    '#required' => TRUE,
    '#default_value' => $config
      ->get('photos_display_full_imagesize'),
    '#options' => $size_options,
    '#suffix' => '</div>',
  ];
  $form['display']['page']['photos_display_page_user'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Allow users to modify this setting when they create a new album.'),
    '#default_value' => $config
      ->get('photos_display_page_user') ?: 0,
    '#options' => [
      $this
        ->t('Disabled'),
      $this
        ->t('Enabled'),
    ],
  ];
  $form['display']['teaser']['photos_display_teaser_display'] = [
    '#type' => 'radios',
    '#default_value' => $config
      ->get('photos_display_teaser_display'),
    '#title' => $this
      ->t('Display setting'),
    '#required' => TRUE,
    '#options' => $display_options,
  ];
  $form['display']['teaser']['photos_display_teaser_viewnum'] = [
    '#type' => 'number',
    '#default_value' => $config
      ->get('photos_display_teaser_viewnum'),
    '#title' => $this
      ->t('Display quantity'),
    '#required' => TRUE,
    '#min' => 1,
    '#step' => 1,
    '#prefix' => '<div class="photos-form-count">',
  ];
  $form['display']['teaser']['photos_display_teaser_imagesize'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Image display size'),
    '#required' => TRUE,
    '#default_value' => $config
      ->get('photos_display_teaser_imagesize'),
    '#options' => $size_options,
    '#suffix' => '</div>',
  ];
  $form['display']['teaser']['photos_display_teaser_user'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Allow users to modify this setting when they create a new album.'),
    '#default_value' => $config
      ->get('photos_display_teaser_user') ?: 0,
    '#options' => [
      $this
        ->t('Disabled'),
      $this
        ->t('Enabled'),
    ],
  ];

  // Count settings.
  $form['count'] = [
    '#title' => $this
      ->t('Statistics'),
    '#type' => 'details',
  ];
  $form['count']['photos_image_count'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Count image views'),
    '#default_value' => $config
      ->get('photos_image_count') ?: 0,
    '#description' => $this
      ->t('Increment a counter each time image is viewed.'),
    '#options' => [
      $this
        ->t('Enabled'),
      $this
        ->t('Disabled'),
    ],
  ];
  $form['count']['photos_user_count_cron'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Image quantity statistics'),
    '#default_value' => $config
      ->get('photos_user_count_cron') ?: 0,
    '#description' => $this
      ->t('Users/Site images and albums quantity statistics.'),
    '#options' => [
      $this
        ->t('Update count when cron runs (affect the count update).'),
      $this
        ->t('Update count when image is uploaded (affect the upload speed).'),
    ],
  ];

  // End count settings.
  // Exif settings.
  $form['exif'] = [
    '#title' => $this
      ->t('Exif Settings'),
    '#type' => 'details',
    '#description' => $this
      ->t('These options require the php exif extension to be loaded.'),
  ];
  $form['exif']['photos_exif'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Show exif information'),
    '#default_value' => $config
      ->get('photos_exif') ?: 0,
    '#description' => $this
      ->t('When the image is available automatically read and display exif information.'),
    '#options' => [
      $this
        ->t('Disabled'),
      $this
        ->t('Enabled'),
    ],
    '#disabled' => extension_loaded('exif') ? FALSE : TRUE,
  ];
  $form['exif']['photos_exif_cache'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Cache exif information'),
    '#default_value' => $config
      ->get('photos_exif_cache') ?: 0,
    '#description' => $this
      ->t('Exif information cache can improve access speed.'),
    '#options' => [
      $this
        ->t('Do not cache'),
      $this
        ->t('To database'),
    ],
    '#disabled' => extension_loaded('exif') ? FALSE : TRUE,
  ];

  // End exif settings.
  if ($module_photos_access_exists) {
    $form['privacy'] = [
      '#title' => $this
        ->t('Privacy settings'),
      '#type' => 'details',
    ];
    $form['privacy']['photos_private_file_styles'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Delete private image styles'),
      '#default_value' => $config
        ->get('photos_private_file_styles') ?: 'automatic',
      '#description' => $this
        ->t('Automatically delete to save disk space. Never delete to improve load speed.'),
      '#options' => [
        'automatic' => $this
          ->t('Automatically delete'),
        'never' => $this
          ->t('Never delete'),
      ],
      '#disabled' => $module_photos_access_exists ? FALSE : TRUE,
    ];
  }
  return parent::buildForm($form, $form_state);
}