Merge pull request #23939 from civicrm/5.51
[civicrm-core.git] / CRM / Core / BAO / File.php
index dc2e7ac0266490e19c5967cd40a2fdbead633796..150c3b6e31cfd96317cfbd48b0d5cec4f6363127 100644 (file)
@@ -428,6 +428,8 @@ AND       CEF.entity_id    = %2";
    * @param int $entityID
    * @param null $numAttachments
    * @param bool $ajaxDelete
+   *
+   * @throws \CRM_Core_Exception
    */
   public static function buildAttachment(&$form, $entityTable, $entityID = NULL, $numAttachments = NULL, $ajaxDelete = FALSE) {
 
@@ -442,15 +444,11 @@ AND       CEF.entity_id    = %2";
     $maxFileSize = $config->maxFileSize ? $config->maxFileSize : 2;
 
     $currentAttachmentInfo = self::getEntityFile($entityTable, $entityID, TRUE);
-    $totalAttachments = 0;
+    $totalAttachments = $currentAttachmentInfo ? count($currentAttachmentInfo) : 0;
     if ($currentAttachmentInfo) {
-      $totalAttachments = count($currentAttachmentInfo);
       $form->add('checkbox', 'is_delete_attachment', ts('Delete All Attachment(s)'));
-      $form->assign('currentAttachmentInfo', $currentAttachmentInfo);
-    }
-    else {
-      $form->assign('currentAttachmentInfo', NULL);
     }
+    $form->assign('currentAttachmentInfo', $currentAttachmentInfo);
 
     if ($totalAttachments) {
       if ($totalAttachments >= $numAttachments) {
@@ -487,8 +485,9 @@ AND       CEF.entity_id    = %2";
         'placeholder' => ts('Description'),
       ]);
 
+      $tagField = "tag_$i";
       if (!empty($tags)) {
-        $form->add('select', "tag_$i", ts('Tags'), $tags, FALSE,
+        $form->add('select', $tagField, ts('Tags'), $tags, FALSE,
           [
             'id' => "tags_$i",
             'multiple' => 'multiple',
@@ -497,6 +496,9 @@ AND       CEF.entity_id    = %2";
           ]
         );
       }
+      else {
+        $form->addOptionalQuickFormElement($tagField);
+      }
       CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_file', NULL, FALSE, TRUE, "file_taglist_$i");
     }
   }