function permissions_by_term_update_8152 in Permissions by Term 8.2
Same name and namespace in other branches
- 8 permissions_by_term.install \permissions_by_term_update_8152()
Add langcode to each permission restriction dataset. Cleanup dangling datasets, with no relation to any taxonomy term.
File
- ./
permissions_by_term.install, line 161 - Install, update and uninstall functions for the permissions_by_term module.
Code
function permissions_by_term_update_8152() {
/**
* @var Connection $database
*/
$database = \Drupal::service('database');
$userTerms = $database
->query("SELECT tid FROM {permissions_by_term_user} WHERE langcode = '' OR langcode IS NULL")
->fetchAll();
$roleTerms = $database
->query("SELECT tid FROM {permissions_by_term_role} WHERE langcode = '' OR langcode IS NULL")
->fetchAll();
foreach ($roleTerms as $roleTerm) {
if (!empty($term = Term::load($roleTerm->tid))) {
$termLangcode = $term
->get('langcode')
->getLangcode();
if (!empty($termLangcode)) {
$database
->query("UPDATE {permissions_by_term_role} SET langcode = :langcode WHERE tid = :tid", [
':langcode' => $termLangcode,
':tid' => $roleTerm->tid,
]);
}
}
else {
$database
->query("DELETE FROM {permissions_by_term_role} WHERE tid = :tid", [
':tid' => $roleTerm->tid,
]);
}
}
foreach ($userTerms as $userTerm) {
if (!empty($term = Term::load($userTerm->tid))) {
$termLangcode = $term
->get('langcode')
->getLangcode();
if (!empty($termLangcode)) {
$database
->query("UPDATE {permissions_by_term_user} SET langcode = :langcode WHERE tid = :tid", [
':langcode' => $termLangcode,
':tid' => $userTerm->tid,
]);
}
}
else {
$database
->query("DELETE FROM {permissions_by_term_user} WHERE tid = :tid", [
':tid' => $userTerm->tid,
]);
}
}
}