2

I currently have a custom module that lets you create an entity. The list page for this has gotten to be really long so I decided to create a view for this to allow filtering. If this were a new module that I was about to install then I could just throw the .yml file into config/staging, but since it has to be done as an update I cannot just put the .yml file there.

It looks like it needs to be created all in code in an update hook.

Is there an easier way of doing this?

4
  • Maybe you want to check out this module called config_update. It looks like that it would be useful. Another solution might be here. This guide describes how to update using hook_update_N() that is a bit more manual way but maybe the best solution.
    – user72672
    Jan 8 '19 at 19:23
  • It sort of is but sort of isn't - this one is specifically asking how to create a View programmatically, and even though the other question and your answer here do provide an alternative approach, we could still get some good answers here to create the View programmatically. That would be useful too
    – Clive
    Jan 8 '19 at 19:29
  • Yes I can see how this could be useful, but it seems like the best approach would be to create the view in hook_update_n(). Just need some direction in doing this, as I have not been able to find much online. Jan 8 '19 at 19:32
  • Yes that would be another solution, but a bit more manual. I think that this guide is great. drupal.org/docs/8/api/update-api/…
    – user72672
    Jan 8 '19 at 19:39
3

Place the YAML file in config/optional as you would normally do to create the view on install optionally when Views is enabled.

Then use an update hook to check whether the view exists when updating the module and create it in PHP by reading the YAML file:

function dblog_post_update_convert_recent_messages_to_view() {

  // Only create if the views module is enabled and the watchdog view doesn't
  // exist.
  if (\Drupal::moduleHandler()
    ->moduleExists('views')) {
    if (!View::load('watchdog')) {

      // Save the watchdog view to config.
      $module_handler = \Drupal::moduleHandler();
      $optional_install_path = $module_handler
        ->getModule('dblog')
        ->getPath() . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY;
      $storage = new FileStorage($optional_install_path);
      \Drupal::entityTypeManager()
        ->getStorage('view')
        ->create($storage
        ->read('views.view.watchdog'))
        ->save();
      return t('The watchdog view has been created.');
    }
    return t("The watchdog view already exists and was not replaced. To replace the 'Recent log messages' with a view, rename the watchdog view and uninstall and install the 'Database Log' module");
  }
}

Source: dblog_post_update_convert_recent_messages_to_view

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.