1

I have an issue with getting the AbstractFieldArray class to work with an image upload for Magento Backend.

Basically I have a settings grid with 4 columns. 1 of them contains a dropdown field, the others are text only.

This is my code to create this LinksArray.php:

namespace Vendor\Module\Block\System\Config\Form\Field;

class LinksArray extends \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray {

    protected $_columns = [];

    protected $_showRenderer;

    protected $_addAfter = true;
    protected $_addButtonLabel;

    protected function _construct() {
        parent::_construct();
        $this->_addButtonLabel = __('Add');
    }

    protected function getShowRenderer() {
        if (!$this->_showRenderer) {
            $this->_showRenderer = $this->getLayout()->createBlock(
                '\Vendor\Module\Block\Adminhtml\Form\Field\Show', '', ['data' => ['is_render_to_js_template' => true]]
            );
        }
        return $this->_showRenderer;
    }

    protected function _prepareToRender() {
        $this->addColumn('show', [ 'label' => __('Anzeigen'), 'renderer' => $this->getShowRenderer() ]);
        $this->addColumn('label', [ 'label' => __('Label'), 'renderer' => false ]);
        $this->addColumn('link_url', [ 'label' => __('Link URL'), 'renderer' => false ]);
        $this->addColumn('icon_url', [ 'label' => __('Icon URL'), 'renderer' => false ]);
        $this->_addAfter = false;
        $this->_addButtonLabel = __('Add');
    }

    protected function _prepareArrayRow(\Magento\Framework\DataObject $row) {
        $options = [];
        $showPicker = $row->_data['show'];
        if ($showPicker) {
            $options['option_' . $this->getShowRenderer()->calcOptionHash($showPicker)] = 'selected="selected"';
        }
        $row->setData('option_extra_attrs', $options);
    }

    public function renderCellTemplate($columnName)
    {
        if ($columnName == "label") {
            $this->_columns[$columnName]['class'] = 'input-text required-entry input-text-array-hover-module input-text-array-hover-label';
            $this->_columns[$columnName]['style'] = '';
        }
        if ($columnName == "link_url") {
            $this->_columns[$columnName]['class'] = 'input-text required-entry input-text-array-hover-module input-text-array-hover-link_url';
            $this->_columns[$columnName]['style'] = '';
        }
        if ($columnName == "icon_url") {
            $this->_columns[$columnName]['class'] = 'input-text required-entry input-text-array-hover-module input-text-array-hover-icon_url';
            $this->_columns[$columnName]['style'] = '';
        }

        return parent::renderCellTemplate($columnName);
    }
}

The "Show" Class from the getShowRenderer() function is defined in this file Show.php:

namespace `Vendor\Module\Block\Adminhtml\Form\Field`;

class Show extends `\Magento\Framework\View\Element\Html\Select` {

    /**
     * methodList
     *
     * @var array
     */
    protected $groupfactory;

    /**
     * Constructor
     *
     * @param \Magento\Framework\View\Element\Context $context
     * @param \Magento\Braintree\Model\System\Config\Source\Country $countrySource
     * @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory
     * @param array $data
     */
    public function __construct(
    \Magento\Framework\View\Element\Context $context, \Magento\Customer\Model\GroupFactory $groupfactory, array $data = []
    ) {
        parent::__construct($context, $data);
        $this->groupfactory = $groupfactory;
    }

    /**
     * Returns countries array
     *
     * @return array
     */

    /**
     * Render block HTML
     *
     * @return string
     */
    public function _toHtml() {
        $options = ['Ja', 'Nein'];

        if (!$this->getOptions()) {
            foreach($options as $option){
                $this->addOption($option, $option);
            }
        }
        return parent::_toHtml();
    }

    /**
     * Sets name for input element
     *
     * @param string $value
     * @return $this
     */
    public function setInputName($value) {
        return $this->setName($value);
    }
}

But I could not manage to add an Image Upload to this. I have a model class for the actual upload, but I do not know how to declare a column's input field as image and assign the model Class to it.

I am thankful for any kind of help.

  • I need to do the same...have you been able to solve this problem? – jennymo Mar 22 '18 at 8:02
  • I'd like to know this too. I tried to render Magento\Config\Block\System\Config\Form\Field, but this can't be done since it is no AbstractBlock (of course). Any idea? – TommyK Jan 16 at 14:02
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review – Rama Chandran M Jan 16 at 14:50

Your Answer

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

Browse other questions tagged or ask your own question.