0

I've created a custom module for a distributor search with a php class/block inside.

Module Structure:

- /Distributor Search
    - /Block
         - DistributorSearch.php
    - /etc
        - module.xml
    - /view
         - /frontend
              - /template
                  - distributor-search.phtml
    - registration.php

I've tried to reference the DistributorSearch block when adding my phtml to the page in my default.xml in my theme.

<block class="Autosmart\DistributorSearch\Block\DistributorSearch" name="distributor.search" template="Autosmart_DistributorSearch::distributor-search.phtml"></block>

I've also tried to keep the phtml file within my theme and include like:

 <block class="Autosmart\DistributorSearch\Block\DistributorSearch" name="distributor.search" template="Magento_Theme::html/distributor-search.phtml"></block>

However I keep getting 500 errors and struggling to get the phtml to show on the page using our module class/block.

I can get the phtml to show on the page by but then I cannot use our methods inside my class DistributorSearch.php?

<block class="Magento\Framework\View\Element\Template" name="distributor.search" template="Autosmart_DistributorSearch::distributor-search.phtml"></block>

Any help to get this working would be appreciated.

UPDATE:

Block class content:

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Autosmart\DistributorSearch\Block;

/**
 * Local Distributor Search Header Block
 */
class DistributorSearch extends \Magento\Framework\App\Config\ScopeConfigInterface
{
    /**
     * Retrieve store name
     *
     * @return string
     */
    public function getStoreName()
    {
        return "testx";
    }
}

distributor-search.phtml contents:

<?php 
echo $block->getStoreName();
?>
Test Content 
  • 1
    is registeration.php a typo or the name of your file? Should be registration.php. – Giel Berkers Jun 22 '17 at 13:07
  • Sorry thats a typo in my question, the module has correct and is registered and enabled. – heady12 Jun 22 '17 at 13:08
  • Can you show more insight in what kind of error the 500 error is? Usually you can see this in your servers' error log. – Giel Berkers Jun 22 '17 at 13:08
  • [Thu Jun 22 13:59:37 2017] [error] [client ::1] FastCGI: comm with server "/Applications/MAMP/fcgi-bin/php7.0.13.fcgi" aborted: idle timeout (30 sec) [Thu Jun 22 13:59:37 2017] [error] [client ::1] FastCGI: incomplete headers (0 bytes) received from server "/Applications/MAMP/fcgi-bin/php7.0.13.fcgi" – heady12 Jun 22 '17 at 13:20
  • ^ Thats whats in my error log, just breaks when I reference the block class as Autosmart\DistributorSearch\Block\DistributorSearch – heady12 Jun 22 '17 at 13:21
2

My best guess is that your block is extending \Magento\Framework\App\Config\ScopeConfigInterface instead of \Magento\Framework\View\Element\Template.

A block should always be a child of \Magento\Framework\View\Element\AbstractBlock, since it contains certain logic to render the HTML and handle layout updates.

I think you're getting the 500 error because your block class is not a Block.

  • Just to confirm, do all module blocks have to extent \Magento\Framework\View\Element\Template? How would you extend other classes? – heady12 Jun 22 '17 at 13:36
  • This gets the phtml to show on my page but unsure how would I use this function in my phtml? I've tried echo $block->getStoreName(); and echo $this->getStoreName(); but cant get the ''testx" to show? I only have this in my phtml file currently? Thanks for you help. – heady12 Jun 22 '17 at 13:39
  • Yes and no. They all should extend \Magento\Framework\View\Element\AbstractBlock or a child of that. \Magento\Framework\View\Element\Template only adds functionality to work with .phtml files. If you want to add functionality, create your own block that extends \Magento\Framework\View\Element\Template (inheritance) and use dependency injection to add the functionality to the block that you want to use in your .phtml files (composition). – Giel Berkers Jun 22 '17 at 13:41
  • Please share your .phtml file in your question – Giel Berkers Jun 22 '17 at 13:42
  • $block->getStoreName() should return "testx" since $block is the reference to your block class. $this in a template is a reference to the template engine. – Giel Berkers Jun 22 '17 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.