Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.57 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\insa_blocks\Plugin\Block;
  4.  
  5. use Drupal\Core\Block\BlockBase;
  6. use Drupal\media_entity\Entity\Media;
  7. use Drupal\file\Entity\File;
  8. use Drupal\Core\Cache\Cache;
  9. /**
  10.  * Provides a 'Banner' block.
  11.  *
  12.  * @Block(
  13.  *  id = "banner",
  14.  *  admin_label = @Translation("Banner"),
  15.  * )
  16.  */
  17. class Banner extends BlockBase {
  18.  
  19.   /**
  20.    * {@inheritdoc}
  21.    */
  22.   public function build() {
  23.     $build = [];
  24.     // Cas node uniquement.
  25.     if ($node = \Drupal::service('current_route_match')->getParameter('node')) {
  26.       // load the node.
  27.       if (!method_exists($node, 'getType')) {
  28.         $node = \Drupal\node\Entity\Node::load($node);
  29.       }
  30.       $node_type = $node->getType();
  31.       // Vérification si field_banner est renseigné.
  32.       if (!empty($node->field_banner[0]->target_id)) {
  33.         $media_id = $node->field_banner[0]->target_id;
  34.         $media = \Drupal::entityTypeManager()->getStorage('media')->load($media_id);
  35.         $build['banner'] = \Drupal::entityTypeManager()->getViewBuilder('media')->view($media, 'wide');
  36.       }
  37.       else {
  38.         $fid = FALSE;
  39.         // Image par défaut.
  40.         if (!empty(\Drupal::config('insa_config.adminsettings')->get($node_type)[0])) {
  41.           $fid = \Drupal::config('insa_config.adminsettings')->get($node_type)[0];
  42.         }
  43.  
  44.         if ($fid) {
  45.           // Chargement de l'image.
  46.           $file = File::load($fid);
  47.           if ($file) {
  48.             $build['banner'] = [
  49.               '#theme' => 'responsive_image',
  50.               '#uri' => $file->getFileUri(),
  51.               '#responsive_image_style_id' => 'wide',
  52.             ];
  53.           }
  54.         }
  55.       }
  56.     }
  57.  
  58.     return $build;
  59.   }
  60.  
  61.   /**
  62.    *
  63.    */
  64.   public function getCacheTags() {
  65.     // With this when your node change your block will rebuild.
  66.     if ($node = \Drupal::routeMatch()->getParameter('node')) {
  67.       if (method_exists($node, 'id')) {
  68.         // If there is node add its cachetag.
  69.         return Cache::mergeTags(parent::getCacheTags(), array('node:' . $node->id()));  
  70.       } else {
  71.         return Cache::mergeTags(parent::getCacheTags(), array('node:' . $node));  
  72.       }
  73.      
  74.     }
  75.     else {
  76.       // Return default tags instead.
  77.       return parent::getCacheTags();
  78.     }
  79.   }
  80.  
  81.   /**
  82.    *
  83.    */
  84.   public function getCacheContexts() {
  85.     // If you depends on \Drupal::routeMatch()
  86.     // you must set context of this block with 'route' context tag.
  87.     // Every new route this block will rebuild.
  88.     return Cache::mergeContexts(parent::getCacheContexts(), array('route'));
  89.   }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement