[ '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', ], ]; } /** * Implements hook_entity_type_build(). * @inheritdoc */ function filebrowser_entity_type_build(array &$entity_types) { /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */ $entity_types['node']->setFormClass('filebrowser_outline', 'Drupal\filebrowser\FilebrowserConfigForm'); } /** * Whe can define the filebrowser fields for node dir_listing. * but for the time we won't use this. We will continue to use hook_form_alter due to the * the complexity of updating. * * Implements hook_form_BASE_FORM_ID_alter(). * @param $form * @param FormStateInterface $form_state * @param $form_id */ function filebrowser_form_node_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { /** @var NodeInterface $node */ $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'; } } /** * Entity form builder to add the filebrowser information to the node. * @param Entity\EntityTypeBundleInfo $entity_type * @param NodeInterface $entity * @param FormStateInterface $form_state * @param array $form * */ function filebrowser_node_builder($entity_type, NodeInterface $entity, &$form, FormStateInterface $form_state) { $entity->filebrowser = new Filebrowser($form_state->getValue('filebrowser')); // Always save a revision for non-administrators. if (!empty($entity->filebrowser->nid) && !\Drupal::currentUser()->hasPermission('administer nodes')) { // $entity->setNewRevision(); } } /** * Implements hook_ENTITY_TYPE_prepare_form() for node entities. * * We will use this hook to set the extra filebrowser fields for * an existing node. For a new node the node Form will supply de defaults * @inheritdoc */ function filebrowser_node_prepare_form(NodeInterface $node, $operation, FormStateInterface $form_state) { if ($node->getType() == 'dir_listing' && $operation == 'edit') { $node->filebrowser = new Filebrowser($node->id()); } } /** * Implements hook_ENTITY_TYPE_load(). * @inheritdoc */ function filebrowser_node_load($nodes) { /** @var \Drupal\node\Entity\Node $node */ foreach ($nodes as $nid => $node) { if ($node->bundle() == 'dir_listing') { $node->filebrowser = new Filebrowser($nid); } } } /** * Implements hook_ENTITY_TYPE_insert(). * @param Drupal\node\NodeInterface $node */ 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'); } } /** * Implements hook_ENTITY_TYPE_update() for node entities. * @var Drupal\node\NodeInterface $node */ function filebrowser_node_update(NodeInterface $node) { /** @var \Drupal\filebrowser\FilebrowserManager $manager */ if ($node->bundle() == 'dir_listing') { $node->filebrowser->nid = $node->id(); $manager = \Drupal::service('filebrowser.manager'); //print_r($node->filebrowser); $manager->updateFilebrowser($node->filebrowser, 'edit'); // invalidate the cache for this node \Drupal\Core\Cache\Cache::invalidateTags(['filebrowser:node:' . $node->id()]); } } /** * Implements hook_ENTITY_TYPE_predelete(). * @inheritdoc */ function filebrowser_node_predelete(NodeInterface $node) { // delete information from filebrowser tables \Drupal::service('filebrowser.common')->nodeDelete($node->id()); } /** * Deletes an Metadata entity when its provider node gets deleted * * Implements hook_entity_delete(). * @inheritdoc */ function filebrowser_node_delete(NodeInterface $node) { if ($node->bundle() == 'dir_listing') { // get all the entities for this node $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(); } } } } /** * Implements hook_ENTITY_TYPE_view(). * @var array $current_view with view_type and theme function * @inheritdoc */ function filebrowser_node_view(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) { /** @var \Drupal\filebrowser\FilebrowserManager $manager */ /** @var \Drupal\filebrowser\Filebrowser $filebrowser*/ if ($view_mode == 'full' && $node->bundle() == 'dir_listing') { $manager = \Drupal::service('filebrowser.manager'); // get the argument $fid from query $fid = \Drupal::request()->query->get('fid'); // we will not use cache $list = new \Drupal\filebrowser\File\DisplayFileList($node, $fid); $display_list = $list->get(); // Insert file listing content part $contents = $manager->createPresentation($node, $display_list); $build['filebrowser_file_list'] = $contents; // Show the statistics of this listing $statistics = new \Drupal\filebrowser\Statistics($display_list); $build['filebrowser_statistics'] = $statistics->get(); $renderer = \Drupal::service('renderer'); $renderer->addCacheableDependency($build, $node); } } /** * Implements hook_node_access. * @inheritdoc */ 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(); } } // todo 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) { // module_load_include("pages.inc", "filebrowser"); // foreach ($nodes as $nid) { // $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid); // _filebrowser_load_files($node); // $fids[]=$node->file_listing['.']['fid']; // } // filebrowser_update_thumbnails($fids); } /** * Implements hook_theme_suggestions_HOOK_alter(). * @inheritdoc */ 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']; } } } } } /** * Implements hook_theme_suggestions_HOOK_alter(). * @inheritdoc */ // Not used, left here for example 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']; } } } } } /** * Prepares variables for container templates. * * Default template: container.html.twig. * * @param array $variables * An associative array containing: * - element: An associative array containing the properties of the element. * Properties used: #id, #attributes, #children. */ function template_preprocess_filebrowser_container_column(&$variables) { $variables['has_parent'] = FALSE; $element = $variables['element']; // Ensure #attributes is set. $element += ['#attributes' => []]; // Special handling for form elements. if (isset($element['#array_parents'])) { // Assign an html ID. if (!isset($element['#attributes']['id'])) { $element['#attributes']['id'] = $element['#id']; } $variables['has_parent'] = TRUE; } $variables['children'] = $element['#children']; $variables['attributes'] = $element['#attributes']; // filebrowser: $variables['width'] = $variables['element']['#attributes']['width']; }