Jelajahi Sumber

Merge pull request #1485 from dbu/alter-new-instance

allow AdminExtension to alter a newly created instance
Thomas 12 tahun lalu
induk
melakukan
82061cdd94

+ 6 - 1
Admin/Admin.php

@@ -1187,7 +1187,12 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
      */
     public function getNewInstance()
     {
-        return $this->getModelManager()->getModelInstance($this->getActiveSubClass() ?: $this->getClass());
+        $object = $this->getModelManager()->getModelInstance($this->getActiveSubClass() ?: $this->getClass());
+        foreach($this->getExtensions() as $extension) {
+            $extension->alterNewInstance($this, $object);
+        }
+
+        return $object;
     }
 
     /**

+ 6 - 0
Admin/AdminExtension.php

@@ -70,4 +70,10 @@ abstract class AdminExtension implements AdminExtensionInterface
      */
     public function configureQuery(AdminInterface $admin, ProxyQueryInterface $query, $context = 'list')
     {}
+
+    /**
+     * {@inheritdoc}
+     */
+    public function alterNewInstance(AdminInterface $admin, $object)
+    {}
 }

+ 8 - 0
Admin/AdminExtensionInterface.php

@@ -91,4 +91,12 @@ interface AdminExtensionInterface
      * @return void
      */
     public function configureQuery(AdminInterface $admin, ProxyQueryInterface $query, $context = 'list');
+
+    /**
+     * Get a chance to modify a newly created instance.
+     *
+     * @param AdminInterface $admin
+     * @param mixed          $object
+     */
+    public function alterNewInstance(AdminInterface $admin, $object);
 }

+ 2 - 2
Admin/AdminInterface.php

@@ -310,7 +310,7 @@ interface AdminInterface
     /**
      * Returns an array of extension related to the current Admin
      *
-     * @return void
+     * @return AdminExtensionInterface[]
      */
     public function getExtensions();
 
@@ -595,7 +595,7 @@ interface AdminInterface
      * @return void
      */
     public function addFormFieldDescription($name, FieldDescriptionInterface $fieldDescription);
-    
+
     /**
      * Returns true if this admin uses ACL
      *

+ 9 - 0
CHANGELOG.md

@@ -1,6 +1,15 @@
 CHANGELOG
 =========
 
+### 2013-07-26
+
+* [BC BREAK] added alterNewInstance to AdminExtensionInterface
+  If you do not extend the AdminExtension, you need to add an empty method to
+  your extension classes:
+
+      public function alterNewInstance(AdminInterface $admin, $object)
+      {}
+
 ### 2013-07-05
 
 *  Remove qTip