function pre_register_unique_entity_presave(EntityInterface $entity) { $allowed_types = ['pre_register_form']; if ($entity->getEntityTypeId() !== 'node' || !in_array($entity->bundle(), $allowed_types, TRUE)) { return; } $request = \Drupal::request(); // ✅ สำคัญมาก: ข้าม OPTIONS if ($request->getMethod() === Request::METHOD_OPTIONS) { return; } if ( $request->getMethod() !== Request::METHOD_POST || !preg_match('#^/(node|jsonapi/node)#', $request->getPathInfo()) ) { return; } $member_id = $entity->get('field_p_register_member_id')->value ?? NULL; $asso_id = $entity->get('field_p_register_associate_id')->value ?? NULL; if (empty($member_id) || empty($asso_id)) { _pre_register_unique_json_error( 400, 'MISSING_REQUIRED_FIELD', 'member_id or associate_id is missing.', compact('member_id', 'asso_id') ); } $query = \Drupal::entityQuery('node') ->condition('type', $entity->bundle()) ->condition('field_p_register_member_id', $member_id) ->condition('field_p_register_associate_id', $asso_id) ->accessCheck(FALSE); // ⛑ ป้องกันเจอตัวเอง if ($entity->id()) { $query->condition('nid', $entity->id(), '<>'); } $duplicate = $query->execute(); if (!empty($duplicate)) { _pre_register_unique_json_error( 400, 'DUPLICATE_MEMBER_ASSOCIATE', 'This member already has this associate registered.', [ 'member_id' => $member_id, 'asso_id' => $asso_id, 'booking_id' => array_values($duplicate), ] ); } // ✅ ผ่าน = ไม่ส่ง response ใด ๆ } 1st Content | Mazda Backend

1st Content

By programes, 25 กรกฎาคม, 2024

1st Body