|
@@ -180,7 +180,8 @@ class SluggedBehavior extends Behavior {
|
|
|
/**
|
|
/**
|
|
|
* SluggedBehavior::slug()
|
|
* SluggedBehavior::slug()
|
|
|
*
|
|
*
|
|
|
- * @param Entity $entity
|
|
|
|
|
|
|
+ * @param \Cake\ORM\Entity $entity Entity
|
|
|
|
|
+ * @param array $options Options
|
|
|
* @return void
|
|
* @return void
|
|
|
*/
|
|
*/
|
|
|
public function slug(Entity $entity, array $options = []) {
|
|
public function slug(Entity $entity, array $options = []) {
|
|
@@ -191,12 +192,13 @@ class SluggedBehavior extends Behavior {
|
|
|
if ($overwrite || $entity->isNew() || !$entity->get($this->_config['field'])) {
|
|
if ($overwrite || $entity->isNew() || !$entity->get($this->_config['field'])) {
|
|
|
$slug = [];
|
|
$slug = [];
|
|
|
foreach ((array)$this->_config['label'] as $v) {
|
|
foreach ((array)$this->_config['label'] as $v) {
|
|
|
- $v = $this->generateSlug($entity->get($v), $entity);
|
|
|
|
|
- if ($v) {
|
|
|
|
|
|
|
+ $v = $entity->get($v);
|
|
|
|
|
+ if ($v !== null && $v !== '') {
|
|
|
$slug[] = $v;
|
|
$slug[] = $v;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
$slug = implode($slug, $this->_config['separator']);
|
|
$slug = implode($slug, $this->_config['separator']);
|
|
|
|
|
+ $slug = $this->generateSlug($slug, $entity);
|
|
|
$entity->set($this->_config['field'], $slug);
|
|
$entity->set($this->_config['field'], $slug);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -227,61 +229,6 @@ class SluggedBehavior extends Behavior {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Generate slug method
|
|
|
|
|
- *
|
|
|
|
|
- * If a new row, or overwrite is set to true, check for a change to a label field and add the slug to the data
|
|
|
|
|
- * to be saved
|
|
|
|
|
- *
|
|
|
|
|
- * @deprecated Not in use anymore!
|
|
|
|
|
- * @return void
|
|
|
|
|
- */
|
|
|
|
|
- public function _slug(Entity $entity) {
|
|
|
|
|
- extract($this->_config);
|
|
|
|
|
-
|
|
|
|
|
- $overwrite = $this->config['overwrite'];
|
|
|
|
|
- if (!$overwrite && !$entity->get($overwriteField)) {
|
|
|
|
|
- $overwrite = true;
|
|
|
|
|
- }
|
|
|
|
|
- if ($overwrite || $entity->isNew()) {
|
|
|
|
|
- if ($label) {
|
|
|
|
|
- $somethingToDo = false;
|
|
|
|
|
- foreach ($label as $field) {
|
|
|
|
|
- $alias = $this->_table->alias();
|
|
|
|
|
- if (strpos($field, '.') !== false) {
|
|
|
|
|
- list($alias, $field) = explode('.', $field, 2);
|
|
|
|
|
- }
|
|
|
|
|
- if (isset($Model->data[$alias][$field])) {
|
|
|
|
|
- $somethingToDo = true;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- if (!$somethingToDo) {
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- $slug = [];
|
|
|
|
|
- foreach ($label as $field) {
|
|
|
|
|
- $alias = $this->_table->alias();
|
|
|
|
|
- if (strpos($field, '.')) {
|
|
|
|
|
- list($alias, $field) = explode('.', $field);
|
|
|
|
|
- }
|
|
|
|
|
- if (isset($Model->data[$alias][$field])) {
|
|
|
|
|
- if (is_array($Model->data[$alias][$field])) {
|
|
|
|
|
- return $this->_multiSlug($Model);
|
|
|
|
|
- }
|
|
|
|
|
- $slug[] = $Model->data[$alias][$field];
|
|
|
|
|
- } elseif ($Model->id) {
|
|
|
|
|
- $slug[] = $Model->field($field);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- $slug = implode($slug, $separator);
|
|
|
|
|
- } else {
|
|
|
|
|
- $slug = $this->display($Model);
|
|
|
|
|
- }
|
|
|
|
|
- $slug = $Model->slug($slug);
|
|
|
|
|
- $Model->data[$this->_table->alias()][$slugField] = $slug;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
* Slug method
|
|
* Slug method
|
|
|
*
|
|
*
|
|
|
* For the given string, generate a slug. The replacements used are based on the mode setting, If tidy is false
|
|
* For the given string, generate a slug. The replacements used are based on the mode setting, If tidy is false
|
|
@@ -374,26 +321,6 @@ class SluggedBehavior extends Behavior {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Display method
|
|
|
|
|
- *
|
|
|
|
|
- * Cheat - use find('list') and assume it has been modified such that lists show in the desired format.
|
|
|
|
|
- * First check (since this method is called in beforeSave) if there is data to be saved, and use that
|
|
|
|
|
- * to get the display name
|
|
|
|
|
- * Otherwise, read from the database
|
|
|
|
|
- *
|
|
|
|
|
- * @deprecated Not in use anymore!
|
|
|
|
|
- * @param mixed $id
|
|
|
|
|
- * @return mixed string (the display name) or false
|
|
|
|
|
- */
|
|
|
|
|
- public function display($id) {
|
|
|
|
|
- $conditions = array_merge([
|
|
|
|
|
- $this->_table->alias() . '.' . $this->_table->primaryKey() => $id],
|
|
|
|
|
- $this->_config['scope']);
|
|
|
|
|
- $record = $this->_table->find('first', ['conditions' => $conditions]);
|
|
|
|
|
- return $record->get($this->_table->displayField());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
* ResetSlugs method.
|
|
* ResetSlugs method.
|
|
|
*
|
|
*
|
|
|
* Regenerate all slugs. On large dbs this can take more than 30 seconds - a time
|
|
* Regenerate all slugs. On large dbs this can take more than 30 seconds - a time
|