page_title_visibility-8.x-1.x-dev/page_title_visibility.install
page_title_visibility.install
<?php
/**
* @file
* Installation functions for the Page Title Visibility module.
*/
/**
* Implements hook_install().
*/
function page_title_visibility_install() {
// Set display_page_title for existing nodes.
_page_title_visibility_update_existing();
// This module must be called after some other modules (i.e. Scheduler).
module_set_weight('page_title_visibility', 98);
}
/**
* Helper function to update the existing nodes on install.
*
* We create the field for existing nodes.
*/
function _page_title_visibility_update_existing() {
$connection = \Drupal::service('database');
// Adding default value to each node.
$connection->update('node_field_data')
->fields([
'display_page_title' => 1,
])
->condition('status', 1, '=')
->execute();
// Grab all nodes ids and their current vid.
$query = $connection->query("SELECT nid, vid FROM {node_field_data}");
$nid_vid_array = $query->fetchAll();
foreach ($nid_vid_array as $entity) {
// Adding default value to each node active vid.
$connection->update('node_field_revision')
->fields([
'display_page_title' => 1,
])
->condition('nid', $entity->nid, '=')
->condition('vid', $entity->vid, '=')
->condition('status', 1, '=')
->execute();
}
drupal_flush_all_caches();
}
