View source
<?php
namespace Drupal\photos\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\photos\PhotosImage;
class PhotosRecentImages extends BlockBase {
public function access(AccountInterface $account, $return_as_object = FALSE) {
if ($account
->hasPermission('view photo')) {
$access = AccessResult::allowed();
}
else {
$access = AccessResult::forbidden();
}
return $return_as_object ? $access : $access
->isAllowed();
}
public function build() {
$config = $this
->getConfiguration();
$count = isset($config['image_count']) ? $config['image_count'] : 10;
if ($content = PhotosImage::blockView('latest', $count, 'photos/image')) {
return [
'#markup' => $content,
'#cache' => [
'tags' => [
'photos:image:recent',
],
],
];
}
}
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$config = $this
->getConfiguration();
$options = array_combine([
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
15,
20,
25,
30,
40,
], [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
15,
20,
25,
30,
40,
]);
$form['image_count'] = [
'#type' => 'select',
'#title' => $this
->t('Number of images to display'),
'#options' => $options,
'#default_value' => isset($config['image_count']) ? $config['image_count'] : '',
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this
->setConfigurationValue('image_count', $form_state
->getValue('image_count'));
}
}