fisharebest / webtrees

Online genealogy
https://webtrees.net
GNU General Public License v3.0
471 stars 301 forks source link

Using the same customized view from different modules #3631

Open JustCarmen opened 3 years ago

JustCarmen commented 3 years ago

I've made a (personal) module which changes a part of the the individual page. I've registered the custom view which overwrites the default individual-page.phtml provided by webtrees.

Recently I've downloaded a third party module which also uses a customized version of the individual-page.phtml. It alters a different part though.

I didn't run in this situation before so it never occurred to me that it is apparently not possible to use the same view from different modules. When the other module is loaded the edits made in my own module are ignored.

Is there a way to use both modules with there respective customizations?

ric2016 commented 3 years ago

I don't think there can be a general solution for these conflicts.

One option is to break down the respective view into smaller parts in webtrees itself, where each part is hopefully then targeted by a single custom module only.

Another option is to modify the generated html elsewhere in the custom module. This seems rather hacky. Even without conflicting custom modules, it still may be useful in cases where a minimal adjustment to a complex view is required, without having to keep track of the view's unrelated changes. Deciding to follow this approach may indicate that the respective view is too complex and should be broken up as well.

An example for this is here. In this case I use it to strip header and footer from all edit dialogs, without having to create separate views for each specific dialog. There may be cleaner solutions for this.

JustCarmen commented 3 years ago

I thought so. You use a kind of javascript to achieve what you want. I use it in another module too in a somewhat different way but with the same purpose. It is sometimes more convenient than views.

But to be honest it was precisely this module of yours that I had downloaded and what makes me discover this issue :). I've a small module which alters the carousel on the individual page and so I discovered the conflict because both modules are using the same view.

I can figure this out and work my way around this issue, but what bothers me is the fact that you are not always aware of the available modules and which views are customized. So when a non-technical user downloads multiple modules from multiple designers they could potentially run into a problem when they want to use both.

ric2016 commented 3 years ago

So when a non-technical user downloads multiple modules from multiple designers they could potentially run into a problem when they want to use both.

It should be possible to detect some of these conflicts. At least we could check for replaced views on module initialization and create a warning in case of multiple modules attempting to replace the same view.

JustCarmen commented 3 years ago

At least we could check for replaced views on module initialization and create a warning in case of multiple modules attempting to replace the same view.

Good point. I think this even should be standard behavior for each custom module. Because in theory every module could conflict with another. Any idea how to achieve this?

ric2016 commented 3 years ago

Yes, I meant we should have this in webtrees. A quick and dirty solution is to adapt View.php:

public static function registerCustomView(string $old, string $new): void
    {
        if (str_contains($old, self::NAMESPACE_SEPARATOR) && str_contains($new, self::NAMESPACE_SEPARATOR)) {
            if (array_key_exists($old, self::$replacements)) {
              $previousNew = self::$replacements[$old];
              FlashMessages::addMessage(I18N::translate('You are using conflicting custom modules: The view %1$s is replaced via %2$s and %3$s. The latter module wins.', $old, $previousNew, $new));
            }
            self::$replacements[$old] = $new;
        } else {
            throw new InvalidArgumentException();
        }
    }

This message may be too technical for the average user though (the module names are given indirectly for one thing).

(By the way, activating this currently shows the following conflict between our custom modules: You are using conflicting custom modules: The view '::modules/clippings/show' is replaced via '_jc-theme-justlight_::modules/clippings/show' and '_vesta_clippings_cart_::modules/clippings/show'. The latter module wins. I won't require this custom view anymore after the next webtrees release due to this PR, so this particular conflict will disappear)