Option to set to Enabled by default

Created on 23 August 2024, 6 months ago

It would be great to be able to set the default value to enabled and then to hide the user interface.

Feature request
Status

Active

Version

2.1

Component

Code

Created by

🇺🇸United States drupgirl

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @drupgirl
  • 🇮🇳India arunkumark Coimbatore

    @drupgirl

    If you want hide file access always, simply use below hook and Access controller,

    /**
     * Implements hook_entity_type_alter().
     */
    function your_module_entity_type_alter(&$entity_types) {
      $entity_types['file']->setAccessClass('Drupal\your_module\FileViewAccessControlHandler');
    }
    

    Access handler path: your_module/src/FileViewAccessControlHandler.php

    <?php
    
    namespace Drupal\your_module;
    
    use Drupal\Core\Access\AccessResult;
    use Drupal\Core\Session\AccountInterface;
    use Drupal\Core\Entity\EntityInterface;
    use Drupal\file\FileAccessFormatterControlHandlerInterface;
    use Drupal\file\FileAccessControlHandler;
    
    /**
     * Provides a File access control handler.
     */
    class FileViewAccessControlHandler extends FileAccessControlHandler implements FileAccessFormatterControlHandlerInterface {
    
      /**
       * {@inheritdoc}
       */
      protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
        // Check if the file permission exists or not.
        if ($operation == 'view') {
          return AccessResult::forbidden();
        }
    
        // For other operations, fall back to the parent check.
        return parent::checkAccess($entity, $operation, $account);
      }
    
    }
    
Production build 0.71.5 2024