Score:0

How do I retrieve the tokenised path for the Files Directory setting configured via the fields settings?

sa flag

I'm using this code to retrieve a remote file, during the process of creating a new a media entity.

$url = 'https://example.com/example.jpg';
$image = system_retrieve_file($url, NULL, TRUE);
$entity_array['field_example'] = [
  'target_id' => $image->id(),
];

The second parameter in system_retrieve_file() determines that the file is stored in the public:// directory.

How can I retrieve the tokenised defined in the file fields settings?

Score:0
sa flag

So I had to make a function that I pass a bundle and field name too. Both are in the form of strings. The function then returns a uri with the tokens replaced.

function getFileDirectory($media_type, $field_name) {
    
        // Fetch all field definitions for the media bundle.
        $fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('media', $media_type);
        foreach ($fields as $field => $field_definition) {
            $bundle = $field_definition->getTargetBundle();
            
            // Look for the field we need
            if ($bundle == $media_type && $field == $field_name) {
                
                // Get the settings for the field, in order to start building the directory path
                $settings = $field_definition->getSettings();
                $file_directory_path_tokens = $settings['file_directory'];
                $uri_scheme = $settings['uri_scheme']; 
                
                // Convert tokens in files directory string
                $token = \Drupal::token();
                $directory_path = $token->replace($file_directory_path_tokens);

                // If there is a value for the file directory setting on the file field, ensure the directory exists, 
                // if not create it.
                if ($directory_path) {
                    $directory = $uri_scheme . '://' . $directory_path;
                    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
                    $file_system = \Drupal::service('file_system');
                    $file_system->prepareDirectory($directory, FileSystemInterface:: CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
                    return $directory;
                } 
                // Otherwise just return the uri_scheme ie public:// or private://
                else {
                    return $uri_scheme . '://';
                }  
            }
        }
    }

This is specific for media entities. It wouldn't be too much bother to extend it to be more generic to cover all entities.

I hope this saves someone some pain.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.