View source
<?php
use Drupal\Core\Entity;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\node\NodeInterface;
use Drupal\filebrowser\Filebrowser;
use Drupal\filebrowser\Services\Common;
function filebrowser_theme() {
return [
'filebrowser_icon_svg' => [
'variables' => [
'html' => [],
'data' => [],
],
],
'statistics' => [
'variables' => [
'statistics' => [],
],
'file' => 'filebrowser.theme.inc',
],
'filebrowser_description' => [
'variables' => [
'data' => [],
],
'file' => 'filebrowser.theme.inc',
],
'filebrowser_grid_container' => [
'variables' => [
'options' => [],
],
],
'filebrowser_grid_item' => [
'variables' => [
'data' => [],
],
],
'filebrowser_container_column' => [
'variables' => [],
'template' => 'filebrowser-container-column',
],
'filebrowser_container' => [
'template' => 'filebrowser-container',
'file' => 'filebrowser.theme.inc',
],
];
}
function filebrowser_entity_type_build(array &$entity_types) {
$entity_types['node']
->setFormClass('filebrowser_outline', 'Drupal\\filebrowser\\FilebrowserConfigForm');
}
function filebrowser_entity_extra_field_info() {
$extra = [];
$extra['node']['dir_listing']['display']['filebrowser_file_list'] = [
'label' => t('Filebrowser file listing'),
'description' => t('Show file listing content part.'),
'weight' => 100,
'visible' => TRUE,
];
$extra['node']['dir_listing']['display']['filebrowser_statistics'] = [
'label' => t('Filebrowser statistics'),
'description' => t('Show the statistics of this listing.'),
'weight' => 100,
'visible' => TRUE,
];
return $extra;
}
function filebrowser_form_node_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$node = $form_state
->getFormObject()
->getEntity();
if ($node
->bundle() == 'dir_listing') {
$form = \Drupal::service('filebrowser.manager')
->addFormExtraFields($form, $form_state, $node);
$form['#entity_builders'][] = 'filebrowser_node_builder';
}
}
function filebrowser_node_builder($entity_type, NodeInterface $entity, &$form, FormStateInterface $form_state) {
$entity->filebrowser = new Filebrowser($form_state
->getValue('filebrowser'));
if (!empty($entity->filebrowser->nid) && !\Drupal::currentUser()
->hasPermission('administer nodes')) {
}
}
function filebrowser_node_prepare_form(NodeInterface $node, $operation, FormStateInterface $form_state) {
if ($node
->getType() == 'dir_listing' && $operation == 'edit') {
$node->filebrowser = new Filebrowser($node
->id());
}
}
function filebrowser_node_load($nodes) {
foreach ($nodes as $nid => $node) {
if ($node
->bundle() == 'dir_listing') {
$node->filebrowser = new Filebrowser($nid);
}
}
}
function filebrowser_node_insert(NodeInterface $node) {
if ($node
->bundle() == 'dir_listing') {
$node->filebrowser->nid = $node
->id();
$manager = \Drupal::service('filebrowser.manager');
$manager
->updateFilebrowser($node->filebrowser, 'insert');
}
}
function filebrowser_node_update(NodeInterface $node) {
if ($node
->bundle() == 'dir_listing') {
$node->filebrowser->nid = $node
->id();
$manager = \Drupal::service('filebrowser.manager');
$manager
->updateFilebrowser($node->filebrowser, 'edit');
\Drupal\Core\Cache\Cache::invalidateTags([
'filebrowser:node:' . $node
->id(),
]);
}
}
function filebrowser_node_predelete(NodeInterface $node) {
\Drupal::service('filebrowser.common')
->nodeDelete($node
->id());
}
function filebrowser_node_delete(NodeInterface $node) {
if ($node
->bundle() == 'dir_listing') {
$query = \Drupal::entityQuery('filebrowser_metadata_entity')
->condition('nid', $node
->id(), '=');
$ids = $query
->execute();
$storage = \Drupal::entityTypeManager()
->getStorage('filebrowser_metadata_entity');
$entities = $storage
->loadMultiple($ids);
if ($entities) {
foreach ($entities as $entity) {
$entity
->delete();
}
}
}
}
function filebrowser_node_view(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) {
if ($view_mode == 'full' && $node
->bundle() == 'dir_listing') {
$manager = \Drupal::service('filebrowser.manager');
$fid = \Drupal::request()->query
->get('fid');
$list = new \Drupal\filebrowser\File\DisplayFileList($node, $fid);
$display_list = $list
->get();
if ($display
->getComponent('filebrowser_file_list')) {
$contents = $manager
->createPresentation($node, $display_list);
$build['filebrowser_file_list'] = $contents;
}
if ($display
->getComponent('filebrowser_statistics')) {
$statistics = new \Drupal\filebrowser\Statistics($display_list);
$build['filebrowser_statistics'] = $statistics
->get();
}
$build['#cache']['max-age'] = 0;
\Drupal::service('page_cache_kill_switch')
->trigger();
}
}
function filebrowser_node_access(NodeInterface $node, $op, AccountInterface $account) {
if ($node
->bundle() == 'dir_listing') {
switch ($op) {
case 'view':
return AccessResult::forbiddenIf(!$account
->hasPermission(Common::VIEW_LISTINGS));
case 'create':
return AccessResult::forbiddenIf(!$account
->hasPermission(Common::CREATE_LISTING));
case 'update':
$has_permission = $account
->hasPermission(Common::EDIT_ANY_LISTINGS) || $account
->hasPermission(Common::EDIT_OWN_LISTINGS) && $account
->id() == $node
->getOwnerId();
return AccessResult::forbiddenIf(!$has_permission);
case 'delete':
$has_permission = $account
->hasPermission(Common::DELETE_ANY_LISTINGS) || $account
->hasPermission(Common::DELETE_OWN_LISTINGS) && $account
->id() == $node
->getOwnerId();
return AccessResult::forbiddenIf(!$has_permission);
default:
return AccessResult::neutral();
}
}
else {
return AccessResult::neutral();
}
}
function filebrowser_node_operations() {
$operations = [
'filebrowser_thumbnails' => [
'label' => t('Update filebrowser thumbnails'),
'callback' => 'filebrowser_node_mass_update_thumbnails',
'callback arguments' => [],
],
];
return $operations;
}
function filebrowser_node_mass_update_thumbnails($nodes) {
}
function filebrowser_theme_suggestions_container_alter(array &$suggestions, array $variables) {
if ($node = \Drupal::service('filebrowser.common')
->getNodeFromPath()) {
if ($node
->bundle() == 'dir_listing') {
if (!empty($variables['element']['#attributes']['class'])) {
$classes = $variables['element']['#attributes']['class'];
if (in_array(Common::FILEBROWSER_GRID_CONTAINER_CLASS, $classes)) {
$suggestions = [
'filebrowser_container',
];
}
if (in_array(Common::FILEBROWSER_GRID_CONTAINER_COLUMN_CLASS, $classes)) {
$suggestions = [
'filebrowser_container_column',
];
}
}
}
}
}
function filebrowser_theme_suggestions_form_element_alter(array &$suggestions, array $variables) {
if ($node = \Drupal::service('filebrowser.common')
->getNodeFromPath()) {
if ($node
->bundle() == 'dir_listing') {
if (!empty($variables['element']['#attributes']['class'])) {
$classes = $variables['element']['#attributes']['class'];
if (in_array(Common::FILEBROWSER_GRID_ITEM_CLASS, $classes)) {
$suggestions = [
'filebrowser_form_element',
];
}
}
}
}
}
function template_preprocess_filebrowser_container_column(&$variables) {
$variables['has_parent'] = FALSE;
$element = $variables['element'];
$element += [
'#attributes' => [],
];
if (isset($element['#array_parents'])) {
if (!isset($element['#attributes']['id'])) {
$element['#attributes']['id'] = $element['#id'];
}
$variables['has_parent'] = TRUE;
}
$variables['children'] = $element['#children'];
$variables['attributes'] = $element['#attributes'];
$variables['width'] = $variables['element']['#attributes']['width'];
}