HansSchouten / Laravel-Pagebuilder

A drag and drop pagebuilder to manage pages in any Laravel project
https://www.phpagebuilder.com
MIT License
795 stars 184 forks source link

Extend CreateTheme class #90

Open billiemead opened 3 years ago

billiemead commented 3 years ago

Hello Hans,

I am trying to extend the CreateTheme class but not having any luck. Can I do this using the pagebuilder config, or are the class replacements just for PHPageBuilder classes?

Here is what I have:

config.pagebuilder.class_replacements

'class_replacements' => [ HansSchouten\LaravelPageBuilder\Commands\CreateTheme::class => Balance\Commands\CreateTheme::class, ],

And app\Balance\Commands\CreateTheme.php

namespace Balance\Commands;

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Exception;

class CreateTheme extends \HansSchouten\LaravelPageBuilder\Commands\CreateTheme
{

}

Hope you are doing well, safe and healthy!

Billie

billiemead commented 3 years ago

I tried again to extend a different class but doesn't work either. I dont know what I am doing wrong:

config/pagebuilder.php

    'class_replacements' => [
    PHPageBuilder\ThemeBlock::class => Balance\ThemeBlock::class,
    PHPageBuilder\Modules\GrapesJS\Block\BlockAdapter::class => Balance\BlockAdapter::class,
    ],

app\Balance\ThemeBlock.php

namespace Balance;

class ThemeBlock extends \PHPageBuilder\ThemeBlock {

 public function getThumbPath()
  {
    return $this->theme->getFolder() . '/public/block-thumbs/' . $this->blockSlug . '.jpg';
  }

  public function getThumbUrl()
  {
    return phpb_theme_asset('public/block-thumbs/' . $this->blockSlug . '.jpg');
  }

}

app\Balance\BlockAdapter.php

namespace Balance;

class BlockAdapter extends \PHPageBuilder\Modules\GrapesJS\Block\BlockAdapter {

  public function getBlockManagerArray()
  {
      $content = $this->pageRenderer->renderBlock($this->block->getSlug());

      $img = '';
      if (file_exists( $this->block->getThumbPath() )){
          $img = '<div class="block-thumb"><img src="' . $this->block->getThumbUrl() . '"></div>';
      }

      $data = [
          'label' => $img . $this->getTitle(),
          'category' => $this->getCategory(),
          'content' => $content
      ];

      if (! $img) {
          $iconClass = 'fa fa-bars';
          if ($this->block->get('icon')) {
              $iconClass = $this->block->get('icon');
          }
          $data['attributes'] = ['class' => $iconClass];
      }

      return $data;
  }

}
HansSchouten commented 3 years ago

Hi Billie, class replacements does only work for objects that are instantiated using the phpb_instance method. So if it is not working you can do a quick search through the project and see if this is missing. It is not yet used everywhere at the moment. For BlockAdapter I just committed a change for this.

For the CreateTheme command I think it is better to create a custom command with a different name and then copy/extend the default one.