Skip to content

Instantly share code, notes, and snippets.

@ericthehacker
Created May 2, 2015 18:40
Show Gist options
  • Save ericthehacker/89371104df3255ec415d to your computer and use it in GitHub Desktop.
Save ericthehacker/89371104df3255ec415d to your computer and use it in GitHub Desktop.
Example of around M2 plug-in
<?php
namespace EW\ConfigScopeHints\Model;
use \Magento\Config\Model\Config\Structure\Element\Field;
use \Magento\Framework\Phrase;
class Plugin
{
/** @var \EW\ConfigScopeHints\Helper\Data */
protected $_helper;
/**
* @param \EW\ConfigScopeHints\Helper\Data $helper
*/
public function __construct(\EW\ConfigScopeHints\Helper\Data $helper) {
$this->_helper = $helper;
}
/**
* Intercept core config form block getScopeLabel() method
* to add additional override hints.
*
* @see Magento\Config\Block\System\Config\Form::getScopeLabel()
* @param \Magento\Config\Block\System\Config\Form $form
* @param callable $getScopeLabel
* @param Field $field
* @return Phrase
*/
public function aroundGetScopeLabel(\Magento\Config\Block\System\Config\Form $form, \Closure $getScopeLabel, Field $field)
{
$currentScopeId = null;
switch($form->getScope()) {
case 'websites':
$currentScopeId = $form->getWebsiteCode();
break;
case 'stores':
$currentScopeId = $form->getStoreCode();
break;
}
$overriddenLevels = $this->_helper->getOverridenLevels($field->getPath(), $form->getScope(), $currentScopeId);
/* @var $returnPhrase Phrase */
$labelPhrase = $getScopeLabel($field);
if(!empty($overriddenLevels)) {
$scopeHintText = $labelPhrase . $this->_helper->formatOverriddenScopes($form, $overriddenLevels);
// create new phrase, now that constituent strings are translated individually
$labelPhrase = new Phrase($scopeHintText, $labelPhrase->getArguments());
}
return $labelPhrase;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment