area17 / twill

Twill is an open source CMS toolkit for Laravel that helps developers rapidly create a custom admin console that is intuitive, powerful and flexible. Chat with us on Discord at https://discord.gg/cnWk7EFv8R.
https://twillcms.com
Apache License 2.0
3.71k stars 567 forks source link

fix: edit url for block browser items #2533

Open zeezo887 opened 5 months ago

zeezo887 commented 5 months ago

Description

This resolves the issue where the edit URL of a block browser item fails to function properly when the browser name doesn't match the item module name.

Related Issues

Relates to #2278

Tofandel commented 5 months ago

Also just encountered this issue, the PR doesn't fix it entirely though

getModuleNameByModel() is not working properly if the model has a different name than the module (which it could have)

The fix is

    function getModuleNameByModel($model)
    {
        try {
            return TwillCapsules::getCapsuleForModel($model)->getModule();
        } catch (NoCapsuleFoundException) {
            return Str::plural(lcfirst(class_basename($model)));
        }
    }
    public function getCapsuleForModel(string|TwillModelContract $model): Capsule
    {
        if ($model instanceof TwillModelContract) {
            $model = get_class($model);
        }

        $model = class_basename($model);

        $capsule = $this->getRegisteredCapsules()->first(function (Capsule $capsule) use ($model) {
            return $capsule->getSingular() === $model;
        });

        if (!$capsule) {
            throw new NoCapsuleFoundException($model);
        }

        return $capsule;
    }
Tofandel commented 5 months ago

Even with all this changes, the issue is still not fixed, newly added relations do get the correct link, but not the ones that were loaded when loading the page

image image image

Tofandel commented 5 months ago

There is another

'edit' => $relatedElement->adminEditUrl ?? moduleRoute(
      $moduleName ?? $relation,
      $routePrefix ?? '',
      'edit',
      $relatedElement->id
  ),

In handle browsers that needs to be replaced with 'edit' => $this->getAdminUrl($relatedElement)

After that it works correctly

zeezo887 commented 5 months ago

@Tofandel Thanks for this! I didn't consider that a module name could be different from the model. Your suggested changes worked fine.

Tofandel commented 5 months ago

Thanks, there is still this https://github.com/area17/twill/pull/2533/files#diff-3b134878262a01ad247d37cebcf578142d170ece67db85d1c31b8d0831c29c71R204-R208 To change according to my last comment