I need help on a custom block on v9

Hello,
I have a custom block to display text, link and an image but in 9 version, image is not showing. In version 8, it works.

This is my view.php

<?php defined('C5_EXECUTE') or die("Access Denied.");
  $v = View::getInstance();
  $themePath = $v->getThemePath();
if ($image) {
    $image->addClass("card-img-top img-fluid");
  }
  
?>
<div class="card mb-5">
  <?php 
    if ($image){
      if ($Pin){ 
        echo '<div class="card-pinned">';
      }
      echo $image;
      if ($Pin){
        echo '<span class="card-pinned-top-end">
            <span class="badge bg-primary rounded-pill">'.$Pin.'</span>
          </span></div>';
      }
    } 
  ?>
  <div class="card-body">
      <?php if($sousTitre):?>
        <span class="card-subtitle"><?php echo h($sousTitre); ?></span>
      <?php endif; ?>
      <?php if($Titre):?>
        <h3 class="card-title"><?php echo h($Titre); ?></h3>
      <?php endif; ?>
    
    <?php if ($paragraph): ?>
		  <span class="card-text">
        <?php echo $paragraph; ?>
		  </span>
    <?php endif; ?>
    
    <?php
      if ($linkURL): ?>
          <a class="card-link" href="<?php echo $linkURL ?>"><?php echo $titreBouton;?></a>
    <?php endif; ?>
  </div>
</div>

And this in my controller.php

public function view()
    {
        $this->set('paragraph', LinkAbstractor::translateFrom($this->paragraph));
        $this->set('linkURL', $this->getLinkURL());
        
        $image = false;
        if ($this->fID) {
            $f = \File::getByID($this->fID);
            if (is_object($f)) {
                $image = Core::make('html/image', array($f, false))->getTag();
                $image->alt($this->getAltText());
                $image->addClass("card-img-top");
                $this->set('image', $image);
            }
        }   
    }

How to adapt the code for the 9 version?

If you put some echo statements in there, is $this->fID returning what you expect? When you get the file by that ID does it return an object? Knowing where it’s failing would make it easier to help with fixing it.

<?php echo $this->fID;?>

return nothing.
Capture d’écran 2022-07-20 à 15.57.23

This is wrong

In v9 when using the make() method you have to specify parameters names like so

$image = Core::make('html/image', array('f' => $f, 'options' => false))->getTag();

You get the parameter names from the __construct method of the class, in this case it’s in concrete\src\Html\Image.php

3 Likes

Thank you ! It seems that works too

use Concrete\Core\Support\Facade\Application;
$app = Application::getFacadeApplication();
$image = $app->make('html/image', ['f' => $f]);

I don’t understand yet what is FacadeApplication

it’s a way of instantiating new object with benefits so to say.
When you do it that way you get a whole lot of possibilities with instantiating classes and interacting with them you don’t get when doing a simple new object()

oh and yes, the options are not required.

If you want to know they can be either a boolean (but that’s deprecated) or an array of options.
If using a boolean:

  • TRUE: asks for a picture tag to be used
  • FALSE: asks for an img tag to be used

Otherwise, the better way is to use an array with the following keys:

  • usePictureTag: true or false or null. TRUE (use a picture tag), FALSE (use an img tag), NULL or not specified use the settings from the current page theme
  • lazyLoadNative: If TRUE the loading="lazy" attribute will be added to the img
  • lazyLoadJavaScript: If TRUE set the img src and/or source srcset image file path to data-src and/or data-srcset

So with options we have that for example?

$image = $app->make('html/image', array('f' => $f, usePictureTag =>'false' ,  lazyLoadNative =>'true', lazyLoadJavaScript =>'true'));

is that correct?

Almost. $options should also be an array so


$image = $app->make('html/image', array('f' => $f, 'options' => array('usePictureTag' => false ,  'lazyLoadNative' => true, 'lazyLoadJavaScript' => true)));

Furthermore, very importantly, never put Boolean values true and false between quotes; otherwise they become strings, and they all become true.

So 'false' is actually true while false if false