Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make doIt function public #11

Closed
Anubarak opened this issue Aug 23, 2019 · 1 comment
Closed

Make doIt function public #11

Anubarak opened this issue Aug 23, 2019 · 1 comment
Labels

Comments

@Anubarak
Copy link

I found a strange issue or at least a very edge case scenario in your plugin.
When there is an internal call in Craft for

Craft::$app->getFields()->getAllFields();

and there are no fields yet, it will fetch all of them from the DB and loop those results to populate fields.
So it will call

public function createField($config): FieldInterface

and it does

$field = ComponentHelper::createComponent($config, FieldInterface::class);

That function tries to validate it

static::validateComponentClass($class, $instanceOf, true);

Remeber $this->fields is still [] because no field is created yet ¯_(ツ)_/¯
Unfortunately if it comes really from a plugin, Craft will do the following

// If it comes from a plugin, make sure the plugin is installed
$pluginsService = Craft::$app->getPlugins();
$pluginHandle = $pluginsService->getPluginHandleByClass($class);
if ($pluginHandle !== null && !$pluginsService->isPluginEnabled($pluginHandle)) {
    /**
     * Returns whether a given plugin is installed and enabled.
     *
     * @param string $handle The plugin handle
     * @return bool
     */
    public function isPluginEnabled(string $handle): bool
    {
        $this->loadPlugins();
        return isset($this->_enabledPluginInfo[$handle]);
    }

The function $this->loadPlugins(); is the key point here.... because it does what the name let's you think.. it loads all plugins and trigger the event

if ($this->hasEventHandlers(self::EVENT_AFTER_LOAD_PLUGINS)) {
    $this->trigger(self::EVENT_AFTER_LOAD_PLUGINS);
}

Remeber when I told $this->fields is still an empty array? That's even now the case when your event is triggert

// Handler: EVENT_AFTER_LOAD_PLUGINS
Event::on(
    Plugins::class,
    Plugins::EVENT_AFTER_LOAD_PLUGINS,
    function () {
        $this->doIt();
    }
);

So $fields = Craft::$app->getFields()->getAllFields(); returns an empty array and no field will be marked with a wheel.
Currently my work-around is to copy your code in my custom module and run it again, but it would be easier to call the function doIt twice

@mmikkel
Copy link
Owner

mmikkel commented Feb 4, 2020

I don't believe this is relevant anymore since I moved the Craft::$app->getUser()->getIsAdmin() query into a EVENT_AFTER_LOAD_PLUGINS handler w/ CP Field Inspect 1.0.7. Closing.

@mmikkel mmikkel closed this as completed Feb 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants