localgovdrupal / localgov_elections

This module provides submodules, content types, views and configuration that allow the reporting of election results for the LocalGov Drupal distribution.
GNU General Public License v2.0
1 stars 0 forks source link

Update chart area view to be more robust when candidates have no surn… #93

Closed finnlewis closed 2 months ago

finnlewis commented 2 months ago

…ame.

See #92

finnlewis commented 2 months ago

Shall we write an update hook then?

Anyone got an example of loading a view from a config file and updating it in an update hook?

chriswales95 commented 2 months ago

Shall we write an update hook then?

Anyone got an example of loading a view from a config file and updating it in an update hook?

Oh I do I think.

This one is from the last version of the module which could be repurposed:

/**
 * Update views that were missing validation.
 */
function localgov_elections_reporting_update_9002() {
    $module_path = \Drupal::getContainer()
        ->get('module_handler')
        ->getModule('localgov_elections_reporting')
        ->getPath();

    $config_to_update = [
        'views.view.electoral_candidates',
        ...
    ];

    $install_dir = new FileStorage($module_path . '/config/install/');
    foreach ($config_to_update as $config){
        $data = $install_dir->read($config);
        $config_obj = \Drupal::configFactory()->getEditable($config);
        $config_obj->setData($data);
        $config_obj->save(TRUE);
    }

    Drupal::logger('localgov_elections_reporting')->notice("Updated views with validation");

    drupal_flush_all_caches();
}

It just needs adjusted for a single view. Only issue with this approach is that it could overwrite a user's changes but I think it's okay given we're still in beta.

chriswales95 commented 2 months ago

@finnlewis I've added the update hook. Let me know if you're happy with it so we can merge.