Просмотр исходного кода

Automatically parsing appropriate Entities for Entity Type in HeartbeatTypeForm
ajax callback retrieves bundles for Entity Type, but does not yet update the form

logicp 8 лет назад
Родитель
Сommit
71830a6605
2 измененных файлов с 56 добавлено и 2 удалено
  1. 12 1
      src/Entity/Heartbeat.php
  2. 44 1
      src/Form/HeartbeatTypeForm.php

+ 12 - 1
src/Entity/Heartbeat.php

@@ -485,9 +485,20 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
   public static function getEntityNames($entityTypes) {
     $names = array();
     foreach ($entityTypes as $type) {
-      $names[] = $type->getLabel()->getUntranslatedString();
+
+      if (($type->getBaseTable() === 'node') ||
+          ($type->getBaseTable() === 'user')
+        ||
+          ($type->getStorageClass() !== NULL &&
+            strpos($type->getStorageClass(), $type->getLabel()->getUntranslatedString())
+          )
+      ) {
+        $names[] = $type->id();
+      }
     }
 
+    sort($names);
+
     return $names;
   }
 }

+ 44 - 1
src/Form/HeartbeatTypeForm.php

@@ -125,6 +125,24 @@ class HeartbeatTypeForm extends EntityForm {
       '#options' => array($this->entityTypes
       ),
       '#required' => TRUE,
+      '#ajax' => [
+        'callback' => '::getBundlesForEntity',
+        'event' => 'change',
+        'progress' => array(
+          'type' => 'throbber',
+          'message' => t('Getting bundles'),
+        ),
+      ]
+    );
+
+    $bundles = $form_state->get('entity_bundles');
+
+    $form['entity_bundles'] = array(
+      '#type' => 'select',
+      '#title' => $this->t('Entity Bundles'),
+//      '#default_value' => $heartbeat_type->getEntityType(),
+      '#description' => $this->t("Primary Entity Type for this Heartbeat Type"),
+      '#options' => array($bundles),
     );
 
 
@@ -208,7 +226,7 @@ class HeartbeatTypeForm extends EntityForm {
           '#type' => 'textfield',
           '#title' => t($messageArguments[$i]),
           '#description' => t('Map value to this variable'),
-          '#default_value' =>$variableValue,
+          '#default_value' => $variableValue,
 //          '#ajax' => !$this->treeAdded ? [
 //            'callback' => '::tokenSelectDialog',
 //            'event' => 'focus',
@@ -320,6 +338,31 @@ class HeartbeatTypeForm extends EntityForm {
     return $argsArray;
   }
 
+  /**
+   * Custom form validation to rebuild
+   * Form field for mapping Message Arguments
+   */
+
+  public function getBundlesForEntity(array &$form, FormStateInterface $form_state) {
+
+    $entityType = $this->entityTypes[$form_state->getValue('entity_type')];
+
+    $entity = $this->entityTypeManager->getStorage($entityType);
+    $bundleTypeName = $entity->getEntityType()->getBundleEntityType();
+    $bundles = $this->entityTypeManager->getStorage($bundleTypeName)->loadMultiple();
+    $bundleNames = array();
+
+    foreach ($bundles as $bundle) {
+      $bundleNames = $bundle->id();
+    }
+
+    $form_state->set('entityBundles', $bundleNames);
+    $form_state->setRebuild();
+
+    return $form['entity_bundles'];
+
+  }
+
   public function tokenSelectDialog(array &$form, FormStateInterface $form_state) {
     // Instantiate an AjaxResponse Object to return.
     $ajax_response = new AjaxResponse();