Browse Source

Merge pull request #136 from jadb/patch-1

Add virtual field support to slug labels
Mark S. 10 years ago
parent
commit
4563b96453

+ 1 - 1
src/Model/Behavior/SluggedBehavior.php

@@ -128,7 +128,7 @@ class SluggedBehavior extends Behavior {
 						throw new \Exception('(SluggedBehavior::setup) model ' . $this->_table->$alias->name . ' is missing the field ' . $field .
 							' (specified in the setup for model ' . $this->_table->name . ') ');
 					}
-				} elseif (!$this->_table->hasField($field)) {
+				} elseif (!$this->_table->hasField($field) && !method_exists($this->_table->entityClass(), '_get' . Inflector::classify($field))) {
 					throw new \Exception('(SluggedBehavior::setup) model ' . $this->_table->name . ' is missing the field ' . $field . ' specified in the setup.');
 				}
 			}

+ 16 - 0
tests/TestApp/Model/Entity/SluggedArticle.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace TestApp\Model\Entity;
+
+use Cake\ORM\Entity;
+
+class SluggedArticle extends Entity {
+	/**
+	 * Virtual field
+	 *
+	 * @return string
+	 */
+	protected function _getSpecial() {
+		return 'dereuromark';
+	}
+}

+ 23 - 0
tests/TestCase/Model/Behavior/SluggedBehaviorTest.php

@@ -584,6 +584,29 @@ class SluggedBehaviorTest extends TestCase {
 		$this->assertEquals('Some-Article-12345', $result['slug']);
 	}
 
+	/**
+	 * Test slug generation works with virtual fields.
+	 *
+	 * @return void
+	 */
+	public function testSlugGenerationWithVirualField() {
+		$this->articles->removeBehavior('Slugged');
+		$this->articles->entityClass('\TestApp\Model\Entity\SluggedArticle');
+		$this->articles->addBehavior('Tools.Slugged', [
+			'label' => [
+				'title',
+				'special'
+			],
+		]);
+
+		$data = ['title' => 'Some Article 12345', 'section' => 0];
+
+		$article = $this->articles->newEntity($data);
+		$result = $this->articles->save($article);
+		$this->assertTrue((bool)$result);
+		$this->assertEquals('Some-Article-12345-dereuromark', $result['slug']);
+	}
+
 /**
  * Get a new Entity
  *