0

When I am trying to delete any CMS page or delete any product in the admin backend it throws an error of the Array to String Conversion in directory /vendor/magento/framework/Data/Form/Element/Select.php on line 135 as mentioned in the screenshot below:

enter image description here

If I modify the code line : 135

 $html .= '>' . $this->_escape($option['label']) . '</option>' . "\n";

with

$html .= '>' . $this->_escape(is_array($option['label'])) . '</option>' . "\n";

Then It definitely resolves the issue of Array to string conversion while deleting the pages or products. But It does not show the option value in the product attribute as mentioned in the screenshot below: enter image description here

We have to put the website to the live side but this issue is coming in a way. Can anyone help me sort out this issue? Thanks

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Framework\Data\Form\Element;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Escaper;
use Magento\Framework\Math\Random;
use Magento\Framework\View\Helper\SecureHtmlRenderer;

/**
 * Form select element
 *
 * @api
 * @author      Magento Core Team <core@magentocommerce.com>
 * @since 100.0.2
 */
class Select extends AbstractElement
{
    /**
     * @var SecureHtmlRenderer
     */
    private $secureRenderer;

    /**
     * @var Random
     */
    private $random;

    /**
     * @param Factory $factoryElement
     * @param CollectionFactory $factoryCollection
     * @param Escaper $escaper
     * @param array $data
     * @param SecureHtmlRenderer|null $secureRenderer
     * @param Random|null $random
     */
    public function __construct(
        Factory $factoryElement,
        CollectionFactory $factoryCollection,
        Escaper $escaper,
        $data = [],
        ?SecureHtmlRenderer $secureRenderer = null,
        ?Random $random = null
    ) {
        $secureRenderer = $secureRenderer ?? ObjectManager::getInstance()->get(SecureHtmlRenderer::class);
        $random = $random ?? ObjectManager::getInstance()->get(Random::class);
        parent::__construct($factoryElement, $factoryCollection, $escaper, $data, $secureRenderer, $random);
        $this->setType('select');
        $this->setExtType('combobox');
        $this->_prepareOptions();
        $this->secureRenderer = $secureRenderer;
        $this->random = $random;
    }

    /**
     * Get the element Html.
     *
     * @return string
     */
    public function getElementHtml()
    {
        $this->addClass('select admin__control-select');

        $html = '';
        if ($this->getBeforeElementHtml()) {
            $html .= '<label class="addbefore" for="' .
                $this->getHtmlId() .
                '">' .
                $this->getBeforeElementHtml() .
                '</label>';
        }

        $html .= '<select id="' . $this->getHtmlId() . '" name="' . $this->getName() . '" ' . $this->serialize(
            $this->getHtmlAttributes()
        ) . $this->_getUiId() . '>' . "\n";

        $value = $this->getValue();
        if (!is_array($value)) {
            $value = [$value];
        }

        if ($values = $this->getValues()) {
            foreach ($values as $key => $option) {
                if (!is_array($option)) {
                    $html .= $this->_optionToHtml(['value' => $key, 'label' => $option], $value);
                } elseif (is_array($option['value'])) {
                    $html .= '<optgroup label="' . $option['label'] . '">' . "\n";
                    foreach ($option['value'] as $groupItem) {
                        $html .= $this->_optionToHtml($groupItem, $value);
                    }
                    $html .= '</optgroup>' . "\n";
                } else {
                    $html .= $this->_optionToHtml($option, $value);
                }
            }
        }

        $html .= '</select>' . "\n";
        if ($this->getAfterElementHtml()) {
            $html .= '<label class="addafter" for="' .
                $this->getHtmlId() .
                '">' .
                "\n{$this->getAfterElementHtml()}\n" .
                '</label>' .
                "\n";
        }
        return $html;
    }

    /**
     * Format an option as Html
     *
     * @param array $option
     * @param array $selected
     * @return string
     */
    protected function _optionToHtml($option, $selected)
    {
        if (is_array($option['value'])) {
            $html = '<optgroup label="' . $option['label'] . '">' . "\n";
            foreach ($option['value'] as $groupItem) {
                $html .= $this->_optionToHtml($groupItem, $selected);
            }
            $html .= '</optgroup>' . "\n";
        } else {
            $optionId = 'optId' .$this->random->getRandomString(8);
            $html = '<option value="' . $this->_escape($option['value']) . '" id="' .$optionId .'" ';
            $html .= isset($option['title']) ? 'title="' . $this->_escape($option['title']) . '"' : '';
            if (in_array($option['value'], $selected)) {
                $html .= ' selected="selected"';
            }
            $html .= '>' . $this->_escape(is_array($option['label'])) . '</option>' . "\n";
            if (!empty($option['style'])) {
                $html .= $this->secureRenderer->renderStyleAsTag($option['style'], "#$optionId");
            }
        }
        return $html;
    }

    /**
     * Prepare options.
     *
     * @return void
     */
    protected function _prepareOptions()
    {
        $values = $this->getValues();
        if (empty($values)) {
            $options = $this->getOptions();
            if (is_array($options)) {
                $values = [];
                foreach ($options as $value => $label) {
                    $values[] = ['value' => $value, 'label' => $label];
                }
            } elseif (is_string($options)) {
                $values = [['value' => $options, 'label' => $options]];
            }
            $this->setValues($values);
        }
    }

    /**
     * Get the Html attributes.
     *
     * @return string[]
     */
    public function getHtmlAttributes()
    {
        return [
            'title',
            'class',
            'style',
            'onclick',
            'onchange',
            'disabled',
            'readonly',
            'tabindex',
            'data-form-part',
            'data-role',
            'data-action'
        ];
    }
}
5
  • From which class file you are loading the values? can u check the eav_attribute table record and check the source_model. is it the custom code u have written ? May 29, 2021 at 7:42
  • No, I didn't add any custom code. When I create any page and try to delete that page it throws an error of Notice: Array to string conversion in vendor/magento/framework/Data/Form/Element/Select.php on line 135 I just view the file and convert code line no 135 $html .= '>' . $this->_escape($option['label']) . '</option>' . "\n"; with the $html .= '>' . $this->_escape(is_array($option['label'])) . '</option>' . "\n"; But all product attribute drop down list value is not displaying. May 29, 2021 at 7:52
  • Just check the value of $option which is getting passed in that _optionToHtml method. May 29, 2021 at 7:59
  • I updated my question and added the whole file Select.php. Can you please check the file above? May 29, 2021 at 8:21
  • Can you please check the file above where $option which is getting passed in that _optionToHtml method. ? May 29, 2021 at 8:59

0

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.