From #2600418-9: “create from template” functionality by @tedbow:

Maybe this would be too much work and maybe I am not understanding how this works in d8 yet but.....

In Drupal 7 Calendar the Views templates for Calendar were created in calendar_views_templates which discovered all date properties on entity types and date fields attached to bundles. This meant that every time a site builder added a new date field to any entity a new template option would appear. This would also work if you installed a new module that defined a new Entity Type with a date property such as created or changed.

As it is now this module would have to define a template for every entity date property in core and contrib modules would have to include theirs. Also date fields added will not get there own template.

There are fews ways that could be used to create Views dynamically for each property and date field.

  1. Stick with the d7 way and either use the plugin discovery derivative system create Views from plugins(or config entities)
  2. Look at what migrate is doing in core to create config entities on the fly. I have talked to @phenaproxima who made this but haven't checked it out myself.
  3. Make the Views dynamically. In this manner you would have just 2 templates for Calendar 1 for Entity properties and 1 for fields. When making a new View, View Template would just load the template and just add info for the desired property or field.
  4. Abandon the idea of templates all together and just make the Views programmatically . There could be a form where you the property or field then call new function createCalendarViewForEntityProperty or createCalendarViewForField

I would be in favor of 3 or 4 or some combination of these methods. I don't think the current method of having a 1-to-1 relationship of Views templates and entity properties(I am not sure if fields are working now) will work long term though.

Again I know I jumping into this late and I am not positive I am understand the system fully.

Comments

geertvd created an issue. See original summary.

geertvd’s picture

Issue summary: View changes
geertvd’s picture

Issue summary: View changes
geertvd’s picture

I'd like to look into option 2 first.

geertvd’s picture

Status: Active » Postponed
Related issues: +#2614988: Allow views templates to be more dynamic
geertvd’s picture

Status: Postponed » Active
Related issues: -#2614988: Allow views templates to be more dynamic

Woops, to many tabs open

tedbow’s picture

@geertvd ok. I will look into how Migrate is doing it. As I said I have not checked it out so for all I know they are doing similar to what I am suggesting in item 3.

Either way it is worth looking into.

tedbow’s picture

Ok here what's seems to be happening in Migrate

There is an abstract BuilderBase Plugin class that implements MigrateBuilderInterface.

form MigrateBuilderInterface docs

Builder plugins implement custom logic to generate migration entities from
migration templates. For example, a migration may need to be customized
based on data that's present in the source database; such customization is
implemented by builders.

I think our use is very similar, "migration entities" extend ConfigEntityBase just like Views. So just replace migration with View above and that is what we need.

So for example in Calendar instead of

a migration may need to be customized
* based on data that's present in the source database

We would need to customize Views based datetime property or field(which would include entity type, base table etc)

tedbow’s picture

Status: Active » Needs work
StatusFileSize
new20.86 KB

Ok. Here is the first try at. It is not complete but I wanted to get feedback on the idea.

Basically it makes a new ViewsBuilder Plugin class that other modules will extend to create a new View.

The plugins will have to be created programmatically via implementing ViewsBuilderPluginInterface::createView

I have created a test module views_templates_builder_test which creates a simple node view.

Eventually there will be test to that uses this module to test basically functionality.

Because views_templates_builder_test is under tests/modules it is hidden to turn it on you need to set
$settings['extension_discovery_scan_tests'] = TRUE;
In settings.local.php

How I see this working for an example the Calendar module is.

  1. Create abstract class CalendarViewsBuilderBase
  2. Create 2 classes that extend this class CalendarFeildViewsBuilder and CalendareBaseFieldViewsBuilder
  3. Create 2 Plugin Derivative classes 1 for each in the classes in step 2
  4. The Plugin derivative classes will create 1 plugin for each field or base field
tedbow’s picture

Just want to make clear that #9 I get rid of the Views Template Config Entities. Since these were basically just loaded and then manually changed to Views they were more like cloning Views.

tedbow’s picture

Status: Needs work » Needs review
StatusFileSize
new30.44 KB

Alright added back Views Template Config Entities but they act slightly differently.

Ok I have added a new type of builder defined in ViewsDuplicateBuilderPluginInterface. Builder Plugins will extend ViewsDuplicateBuilderBase.

This type of builder will start with start with View Template and then edit it in the new method alterViewTemplateAfterCreation.

Any changes should be made to the template here. If there are no changes to the template it can be omitted in extended class.

I think Calendar builders should probably actually extend ViewsDuplicateBuilderBase

tedbow’s picture

Ok I don't really like that fact that we are using config entities for Views Templates. I don't think they are config entities in any sense.

I have noticed some problems.

They get exported by CMI. There is probably no reason for this.
If this module doesn't delete the config entity there is problem when you try to re-install the module on the same site.

Basically they aren't used to configure anything on the site and I feel this is just using CMI just to load YML files which shouldn't be that hard.

Here is my idea

  1. Modules that want to define Views Templates will create a folder called views_templates in the base of their directory.
  2. This module creates a service called "views_template.loader"
  3. ViewsDuplicateBuilderBase will use views_template.loader to load templates by looking in the current module "views_templates/[template_id].yml"

Thoughts, other ideas?

tedbow’s picture

Alright right I think this is close to done!

I have removed the Views Templates as config entities because of the problems noted in #12

Now the Views Templates are just plain old yml files. This is actually what Migrate in Drupal 8 core is doing.

I created a new service views_templates.loader that simply loads a template from a yml for a plugin. Since it is service it should be easy change how it works if the plain yml files don't work well enough.

I have also a new method ViewsDuplicateBuilderBase::replaceTemplateKeyAndValues which will replaces values and keys in the template with values from the plugin definition. Derivative classes should be used so that Plugins have different replace values in the plugin definition.

@see ViewDuplicatorTest and it's corresponding yml file for an example of how this works.

tedbow’s picture

Just some fixes to logic after using it with Calendar

  • tedbow committed 2dd253f on 8.x-1.x
    Issue #2614988 by tedbow: Allow views templates to be more dynamic
    
tedbow’s picture

Status: Needs review » Fixed

Ok. I fixed this it. It will probably need to be revisited but it works for now.

  • tedbow committed bfb3bb1 on 8.x-1.x
    Issue #2614988 by tedbow: Allow views templates to be more dynamic
    

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.