Created
May 17, 2020 00:26
-
-
Save michaelgarydean/6c97cf1f1f841511c1e1a6bea5e7aa8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Module file for the portail_reformat_stop_words.module. | |
* | |
* This module is a solution to reformat titles that were input with 'stop words' | |
* between brackets. The idea was that titles shouldn't be sorted using common words like | |
* 'les', 'la' etc. | |
* | |
* The module loads all the nodes from the content type, finds occurances of the stop words, | |
* removes them, and finally adds it to the beginning of the title. Node titles are then updated | |
* in the database. | |
* | |
* @see https://www.wunderlist.com/#/tasks/3026645045 | |
* @author Michael Dean <contact@michaeldean.ca> | |
*/ | |
/** | |
* Remove any remaining hyphens at the beginning of Piece titles. | |
*/ | |
function portail_reformat_stop_words_update_7403() { | |
//Load all the nodes from the piece content type | |
$node_type = "pi_ce"; | |
$piece_nodes = node_load_multiple(array(), array('type' => $node_type)); | |
//Check each node to see if it starts with a hyphen | |
foreach($piece_nodes as $node) { | |
//Only do a node save if a hyphen is removed. | |
if(substr($node->title, 0, 1) === '-') { | |
$node->title = ltrim($node->title,'-'); | |
$node->revision = 1; | |
$node->log = 'Hyphen removed automatically from portail_reformat_stop_words module'; | |
node_save($node); | |
} | |
} | |
} | |
/** | |
* Copy the title field to the field_sorting field | |
*/ | |
function portail_reformat_stop_words_update_7401() { | |
//Load all the nodes from the piece content type | |
$node_type = "pi_ce"; | |
$piece_nodes = node_load_multiple(array(), array('type' => $node_type)); | |
//Copy the title to the sorting field | |
foreach($piece_nodes as $node) { | |
/** | |
* First remove the left-over titles with hyphens from last title reformatting | |
* @see https://www.wunderlist.com/#/tasks/3026645551 | |
*/ | |
$node->title = ltrim($node->title,'-'); | |
//Copy the field | |
$node->field_sorting['und'][0]['value'] = $node->title; | |
//Save the updated node | |
field_attach_update('node', $node); | |
} | |
} | |
/** | |
* Reformat all nodes of type 'piece' so stop words are place at beginning of the titles. | |
*/ | |
function portail_reformat_stop_words_update_7402() { | |
//The stop words to remove and place at the beginning of the title | |
$stop_words = array("L'", "La", "Le", "Les"); | |
//Load all the nodes of a specific content type | |
$node_type = "pi_ce"; | |
$piece_nodes = node_load_multiple(array(), array('type' => $node_type)); | |
//If the node has one of the stop words in it, reformat the title | |
foreach($piece_nodes as $node) { | |
/** | |
* First remove the left-over titles with hyphens from last title reformatting | |
* @see https://www.wunderlist.com/#/tasks/3026645551 | |
*/ | |
$node->title = ltrim($node->title,'-'); | |
$node_title = $node->title; | |
//Check for each stop word. Assumed it only occurs once. | |
foreach($stop_words as $stop_word) { | |
//In the titles, there are parentheses placed around each stop word | |
$stop_word_parentheses = '(' . $stop_word . ')'; | |
//Get the position (not-false) of the stop word if it is found in the title | |
$stop_word_pos = strpos($node_title,$stop_word_parentheses); | |
//Format the title if it contains a stop word in its title | |
if($stop_word_pos !== false) { | |
//Remove the stop word from the title | |
$node_title = str_replace($stop_word_parentheses,'',$node_title); | |
/** | |
* Deal with spacing between stop word and beginning of title | |
* (L') is a special case because it doesn't need a space between the word | |
*/ | |
$space = ' '; | |
if($stop_word === "L'") { | |
$space = ''; | |
} | |
$node_title = $stop_word . $space . $node_title; | |
//Update the node title, add a new revision, prepare for saving | |
$node->title = $node_title; | |
$node->revision = 1; | |
$node->log = 'Stop words removed automatically from portail_reformat_stop_words module'; | |
node_save($node); | |
//Don't bother looking for more stop words in this title. If one is already found, we're done. | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment