Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Contains Drupal\Core\Path\Path.
*/
namespace Drupal\Core\Path;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Connection;
use Drupal\Core\Language\Language;
Dries Buytaert
committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Defines a class for CRUD operations on path aliases.
*/
class Path {
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* Constructs a Path CRUD object.
*
* @param \Drupal\Core\Database\Connection $connection
* A database connection for reading and writing path aliases.
*
* @param \Drupal\Core\Path\AliasManager $alias_manager
* An alias manager with an internal cache of stored aliases.
*
* @todo This class should not take an alias manager in its constructor. Once
* we move to firing an event for CRUD operations instead of invoking a
* hook, we can have a listener that calls cacheClear() on the alias manager.
*/
public function __construct(Connection $connection, AliasManager $alias_manager) {
$this->connection = $connection;
$this->alias_manager = $alias_manager;
}
/**
* Saves a path alias to the database.
*
* @param string $source
* The internal system path.
*
* @param string $alias
* The URL alias.
*
* @param string $langcode
* The language code of the alias.
*
* @param int $pid
* Unique path alias identifier.
*
* @return
* FALSE if the path could not be saved or an associative array containing
* the following keys:
* - source: The internal system path.
* - alias: The URL alias.
* - pid: Unique path alias identifier.
* - langcode: The language code of the alias.
*/
public function save($source, $alias, $langcode = Language::LANGCODE_NOT_SPECIFIED, $pid = NULL) {
Dries Buytaert
committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
$fields = array(
'source' => $source,
'alias' => $alias,
'langcode' => $langcode,
);
// Insert or update the alias.
if (empty($pid)) {
$query = $this->connection->insert('url_alias')
->fields($fields);
$pid = $query->execute();
$fields['pid'] = $pid;
// @todo: Find a correct place to invoke hook_path_insert().
$hook = 'path_insert';
}
else {
$fields['pid'] = $pid;
$query = $this->connection->update('url_alias')
->fields($fields)
->condition('pid', $pid);
$pid = $query->execute();
// @todo: figure out where we can invoke hook_path_update()
$hook = 'path_update';
}
if ($pid) {
// @todo Switch to using an event for this instead of a hook.
module_invoke_all($hook, $fields);
$this->alias_manager->cacheClear();
return $fields;
}
return FALSE;
}
/**
* Fetches a specific URL alias from the database.
*
* @param $conditions
* An array of query conditions.
*
* @return
* FALSE if no alias was found or an associative array containing the
* following keys:
* - source: The internal system path.
* - alias: The URL alias.
* - pid: Unique path alias identifier.
* - langcode: The language code of the alias.
*/
public function load($conditions) {
$select = $this->connection->select('url_alias');
foreach ($conditions as $field => $value) {
$select->condition($field, $value);
}
return $select
->fields('url_alias')
->execute()
->fetchAssoc();
}
/**
* Deletes a URL alias.
*
* @param array $conditions
* An array of criteria.
*/
public function delete($conditions) {
$path = $this->load($conditions);
$query = $this->connection->delete('url_alias');
foreach ($conditions as $field => $value) {
$query->condition($field, $value);
}
$deleted = $query->execute();
// @todo Switch to using an event for this instead of a hook.
module_invoke_all('path_delete', $path);
$this->alias_manager->cacheClear();
return $deleted;
}
}