List all node titles to better iterate on a list of names
Pergunta
I have a content type called Country and I have to verify what countries already exists in db and save the missing ones, comparing to a variable with all countries ($country_list). I am having trouble accessing countries names of my nodes, that are specified as the content type title.
$country_list = ['Mexico', 'Tanzania', 'Brazil', 'Camaron'];
$nodes = node_load_multiple(array(), array('type' => 'country_profile'));
$nodes_name = node_title_list($nodes);
foreach ($country_list as $country_name) {
if (!in_array($nodes_name, $nodes)) {
// should create new node
} else {
// shouldn't do anything
}
}
The problem is, when i get the node_title_list()
it doesn't return the 'raw title' (see output below)
Output:
Array
(
[#theme] => item_list__node
[#items] => Array
(
[0] => <a href="/discover/country_profiles/tanzania">Tanzania</a>
[1] => <a href="/discover/country_profiles/mexico">Mexico</a>
)
[#title] => 1
)
How can i do that, so that the output of this list is
Array
(
[0] => "Tanzania"
[1] => "Mexico"
)
Solução
If you just need to create the nodes that don't have a specific title (which I guess it's just a country name), you don't need to use node_title_list()
, whose purpose is rendering a list of node titles as links. Just load the nodes and check which titles aren't already used.
$country_list = ['Mexico', 'Tanzania', 'Brazil', 'Camaron'];
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'country_profile')
// Remove the following line if you don't need to get only published nodes.
->propertyCondition('status', NODE_PUBLISHED)
// Keep the following line, so Drupal won't check if the currently logged-in user has access to the nodes and the fields.
->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT');
$result = $query->execute();
if (isset($result['node'])) {
$country_list = array_flip($country_list);
$nodes = entity_load('node', array_keys($result['node']));
foreach ($nodes as $node) {
unset($country_list[$node->title]);
}
$country_list = array_flip($country_list);
}
if (!empty($country_list)) {
// There are nodes that need to be created; their titles are in $country_list.
}
Even better, load those nodes whose title is equal to one of the country names you already have.
$country_list = ['Mexico', 'Tanzania', 'Brazil', 'Camaron'];
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'country_profile')
// Remove the following line if you don't need to get only published nodes.
->propertyCondition('status', NODE_PUBLISHED)
->propertyCondition('title', $country_list)
// Keep the following line, so Drupal won't check if the currently logged-in user has access to the nodes and the fields.
->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT');
$result = $query->execute();
if (isset($result['node'])) {
$country_list = array_flip($country_list);
$nodes = entity_load('node', array_keys($result['node']));
foreach ($nodes as $node) {
unset($country_list[$node->title]);
}
$country_list = array_flip($country_list);
}
if (!empty($country_list)) {
// There are nodes that need to be created; their titles are in $country_list.
}