Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Dynamic Block Bundle Laravel Package

awaresoft/dynamic-block-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    • Ensure your project uses Symfony 2.x (compatible with Doctrine ORM 2.x).
    • Install via Composer (if not symlinked):
      composer require awaresoft/dynamic-block-bundle
      
    • Register the bundle in app/AppKernel.php:
      new Awaresoft\DynamicBlockBundle\AwaresoftDynamicBlockBundle(),
      
  2. First Use Case:

    • Create a Dynamic Block: Extend Awaresoft\DynamicBlockBundle\Entity\Block to define custom block types. Example:
      namespace AppBundle\Entity;
      
      use Awaresoft\DynamicBlockBundle\Entity\Block;
      
      class CustomBlock extends Block
      {
          // Add custom fields/methods
      }
      
    • Configure Sonata Admin: Extend sonata_block services in config.yml:
      sonata_block:
          blocks:
              custom_block:
                  class: AppBundle\Block\CustomBlockService
                  contexts: [cms]
      
  3. Database Setup:

    • Run migrations (if using Doctrine):
      php app/console doctrine:schema:update --force
      
    • Load fixtures (if provided):
      php app/console doctrine:fixtures:load
      

Implementation Patterns

Core Workflows

  1. Block Creation & Management:

    • Dynamic Blocks: Use BlockManager to create/manage blocks dynamically:
      $block = $blockManager->create('custom_block', ['title' => 'Hello']);
      $blockManager->save($block);
      
    • Templates: Override default templates in Resources/views/ (e.g., AwaresoftDynamicBlockBundle:Block:block.html.twig).
  2. Sonata Integration:

    • Admin Panels: Extend sonata.admin to manage blocks via UI:
      # config.yml
      sonata_admin:
          options:
              delete:
                  confirmation: "Are you sure you want to delete this block?"
      
    • Block Contexts: Assign blocks to contexts (e.g., cms, dashboard) in sonata_block config.
  3. Reusable Components:

    • Block Services: Create services for complex blocks:
      // src/AppBundle/Block/CustomBlockService.php
      class CustomBlockService extends BlockService
      {
          public function execute(BlockContextInterface $blockContext, Response $response = null)
          {
              // Custom logic
          }
      }
      
    • Register Service:
      services:
          app.custom_block:
              class: AppBundle\Block\CustomBlockService
              tags:
                  - { name: sonata.block }
      
  4. Caching:

    • Enable block caching in config.yml:
      awaresoft_dynamic_block:
          cache: true
          cache_lifetime: 3600
      

Gotchas and Tips

Pitfalls

  1. Symfony 2.x Legacy:

    • Doctrine ORM: Ensure your Entity/Block extends Awaresoft\DynamicBlockBundle\Entity\Block (not Sonata\BlockBundle\Model\Block).
    • Symfony Components: Some features may rely on deprecated Symfony 2.x components (e.g., Twig_Environment). Use polyfills if needed.
  2. Sonata Admin Quirks:

    • Block Permissions: Verify ROLE_SONATA_ADMIN_BLOCK is assigned to users managing blocks.
    • Context Conflicts: Blocks may not render if contexts aren’t properly configured in sonata_block or sonata_admin.
  3. Caching Issues:

    • Cache Invalidation: Manually clear cache after block updates:
      php app/console cache:clear
      
    • Debugging: Disable caching temporarily in config.yml to test live updates.
  4. Database Schema:

    • Migrations: Always run doctrine:schema:update after modifying Block entities to avoid schema mismatches.

Debugging Tips

  1. Log Block Events:

    • Enable debug mode and check app/logs/dev.log for block-related errors.
    • Example log entry:
      $this->get('logger')->debug('Block rendered', ['block_id' => $block->getId()]);
      
  2. Template Overrides:

    • Override templates in app/Resources/AwaresoftDynamicBlockBundle/views/ to avoid merging conflicts.
    • Clear cache after template changes:
      php app/console assetic:dump --watch --env=dev
      
  3. Symlinking Vendors:

    • If symlinked, ensure autoload_psr4.php is updated:
      composer dump-autoload
      

Extension Points

  1. Custom Block Types:

    • Extend Block entity and create a corresponding service (tagged as sonata.block).
    • Example:
      class VideoBlock extends Block
      {
          private $videoUrl;
      
          // Getters/setters
      }
      
  2. Event Listeners:

    • Subscribe to block events (e.g., sonata.block.event.BLOCK_PRE_RENDER):
      // src/AppBundle/EventListener/BlockListener.php
      class BlockListener
      {
          public function onPreRender(BlockEvent $event)
          {
              $block = $event->getBlock();
              // Modify block data
          }
      }
      
      Register in services.yml:
      services:
          app.block_listener:
              class: AppBundle\EventListener\BlockListener
              tags:
                  - { name: kernel.event_listener, event: sonata.block.event.BLOCK_PRE_RENDER, method: onPreRender }
      
  3. Fixtures:

    • Load sample blocks via fixtures:
      // src/AppBundle/DataFixtures/ORM/LoadBlockData.php
      class LoadBlockData extends AbstractFixture
      {
          public function load(ObjectManager $manager)
          {
              $block = new CustomBlock();
              $block->setTitle('Test Block');
              $manager->persist($block);
              $manager->flush();
          }
      }
      
    • Load with:
      php app/console doctrine:fixtures:load
      
  4. API Integration:

    • Expose blocks via API using FOSRestBundle or similar:
      // src/AppBundle/Controller/BlockController.php
      class BlockController extends Controller
      {
          public function getBlocksAction()
          {
              return $this->get('block.manager')->findAll();
          }
      }
      
      Route in routing.yml:
      app_block_api:
          path: /api/blocks
          defaults: { _controller: AppBundle:Block:getBlocks }
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware