0

I need to get theme's Default Title configuration value in my logo.phtml.

So after many searches, I'm using a bad way for do that! I'm using the Object Manager Instance directly in template file!

app\design\frontend\MyVendor\myTheme\Magento_Theme\templates\html\header\logo.phtml:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_pageConfig = $objectManager->get('Magento\Framework\View\Page\Config');
$_metaTitle = strip_tags($_pageConfig->getTitle()->getDefault());

And it works!

But how can I get anything from Configuration Values in overridden templates in proper way like Block Class or Layout stuff?!

By the way, I'm using Magento v2.1.9 with PHP 7.0.24.

-1

You should use Dependency Injection instead of Object Manager Instance. To do so, you should create a new module to extend the core block, then use it inside template.

  1. I assume you know how to create a module. If not, refer to here. The name of module is MyVendor_MyModule in my example.

  2. After creating the basic module structure, create file di.xml under app/code/MyVendor/MyModule/etc with the following content:

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <preference for="Magento\Theme\Block\Html\Header\Logo" type="MyVendor\MyModule\Block\Logo" />
    </config> 
    
  3. Create file Logo.php under app/code/MyVendor/MyModule/Block with the following content:

    <?php
    
    namespace MyVendor\MyModule\Block;
    
    use \Magento\Theme\Block\Html\Header\Logo as Core_Logo;
    use \Magento\Framework\View\Element\Template\Context;
    use \Magento\MediaStorage\Helper\File\Storage\Database;
    use \Magento\Framework\View\Page\Config;
    
    class Logo extends Core_Logo
    {
    
        protected $config;
    
        public function __construct(
            Context $context,
            Database $fileStorageHelper,
            array $data = [],
            Config $config
        ) {
            parent::__construct($context, $fileStorageHelper, $data);
            $this->config = $config;
        }
    
        public function getTitle(){               
            return strip_tags($this->config->getTitle()->getDefault());
        }
    }
    
  4. Enable the module

  5. As we created a new function getTitle(), apply it into your template app\design\frontend\MyVendor\myTheme\Magento_Theme\templates\html\header\logo.phtml using the following code:

        <?php echo $block->getTitle(); ?>
    

More Ref.: How to override blocks in v2.1

| improve this answer | |
  • Yes thanks. I did it earlier. but it not works! after that the logo.phtml not running and theme loads without this logo template! It ignored in some reason... – Mehran Zarei Nov 10 '17 at 9:37
  • Did you clear cache or delete files in static folder? – PY Yick Nov 10 '17 at 14:54
  • Yes, I did. I deleted everything in var and pub/static folder and used php bin/magento setup:upgrade and php bin/magento cache:flush. But nothing changed! – Mehran Zarei Nov 10 '17 at 17:49

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.