You are here

public function Dynamic_Banner_Block::dynamic_banner_image_handler in Dynamic Banner 8

This function will load imgurl if there is no url for img then it will load the fids into path format

Input 1: The imgurl(s) that we are loading [maybe csv] Input 2: The imgfid(s) that we are loading [maybe csv]

1 call to Dynamic_Banner_Block::dynamic_banner_image_handler()
Dynamic_Banner_Block::build in src/Plugin/Block/Dynamic_Banner_Block.php
Builds and returns the renderable array for this block plugin.

File

src/Plugin/Block/Dynamic_Banner_Block.php, line 195

Class

Dynamic_Banner_Block
Provides a 'Hello' Block.

Namespace

Drupal\dynamic_banner\Plugin\Block

Code

public function dynamic_banner_image_handler($imgurl, $imgfid) {

  // we have found the imgurl already in the right format return it
  if ($imgurl && $imgurl != '') {
    return $imgurl;
  }
  else {
    if (strrpos($imgfid, ',')) {

      // split the plain string into an array
      $all_fids = explode(",", $imgfid);

      // load all files at once

      //$all_files = file_load_multiple($all_fids);
      $all_files = \Drupal\file\Entity\File::loadMultiple($all_fids);
      $retval = "";

      // default the return string
      // go into all the loaded files
      foreach ($all_files as $file) {

        // if this is the first time through do not add a comma to the string
        if ($retval != "") {
          $retval .= ",";
        }

        // have to translate the public string in the uri back into something browsers understand
        if (isset($file)) {
          $fileUrl = file_create_url($file
            ->getFileUri());
        }
        else {
          $fileUrl = '';
        }

        //$retval .= str_replace('public://', file_public_path() . '/', $fileUrl);
        $retval .= str_replace('public://', '/', $fileUrl);
      }
      return $retval;
    }
    else {
      $file = \Drupal::service('entity_type.manager')
        ->getStorage('file')
        ->load((int) $imgfid);

      // have to translate the public string in the uri back into something browsers understand
      if (isset($file)) {
        $fileUrl = file_create_url($file
          ->getFileUri());
      }
      else {
        $fileUrl = '';
      }
      $file_path = str_replace('public://', '/', $fileUrl);
      return $file_path;
    }
  }
}