B
B
balex7772020-01-22 13:53:15
Yii
balex777, 2020-01-22 13:53:15

how to override widget method in yii2?

There is a widget in the yii2-authclient extension, here it is specifically here is a widget
I need to redo the method for displaying buttons this public function clientLink
I created another widget with this method like this

<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace frontend\widget;

use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\helpers\Html;
use yii\authclient\ClientInterface;
use yii\authclient\widgets\AuthChoice;
use yii\authclient\widgets\AuthChoiceItem;


class MyAuthChoice extends AuthChoice
{

    /**
     * Outputs client auth link.
     * @param ClientInterface $client external auth client instance.
     * @param string $text link text, if not set - default value will be generated.
     * @param array $htmlOptions link HTML options.
     * @return string generated HTML.
     * @throws InvalidConfigException on wrong configuration.
     */
    public function clientLink($client, $text = null, array $htmlOptions = [])
    {
        $viewOptions = $client->getViewOptions();

        if (empty($viewOptions['widget'])) {
            if ($text === null) {
                $text = Html::tag('span', '', ['class' => 'blabla ' . $client->getName()]);
            }
            if (!isset($htmlOptions['class'])) {
                $htmlOptions['class'] = $client->getName();
            }
            if (!isset($htmlOptions['title'])) {
                $htmlOptions['title'] = $client->getTitle();
            }
            Html::addCssClass($htmlOptions, ['widget' => 'auth-link']);

            if ($this->popupMode) {
                if (isset($viewOptions['popupWidth'])) {
                    $htmlOptions['data-popup-width'] = $viewOptions['popupWidth'];
                }
                if (isset($viewOptions['popupHeight'])) {
                    $htmlOptions['data-popup-height'] = $viewOptions['popupHeight'];
                }
            }
            return Html::a($text, $this->createClientUrl($client), $htmlOptions);
        }

        $widgetConfig = $viewOptions['widget'];
        if (!isset($widgetConfig['class'])) {
            throw new InvalidConfigException('Widget config "class" parameter is missing');
        }
        /* @var $widgetClass Widget */
        $widgetClass = $widgetConfig['class'];
        if (!(is_subclass_of($widgetClass, AuthChoiceItem::AuthChoice))) {
            throw new InvalidConfigException('Item widget class must be subclass of "' . AuthChoiceItem::AuthChoice . '"');
        }
        unset($widgetConfig['class']);
        $widgetConfig['client'] = $client;
        $widgetConfig['authChoice'] = $this;
        return $widgetClass::widget($widgetConfig);
    }


}

Leg widget is not working now, why? although with vardump that the AuthChoice widget and MyAuthChoice show the same object
What's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2020-01-23
@balex777

Can you write down what you changed? Everyone has to compare your code with the original?
I don't think this part of the code is correct:

$widgetClass = $widgetConfig['class'];
        if (!(is_subclass_of($widgetClass, AuthChoiceItem::AuthChoice))) {
            throw new InvalidConfigException('Item widget class must be subclass of "' . AuthChoiceItem::AuthChoice . '"');
        }

Must be
AuthChoiceItem::class

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question