From af80b0872e8f096fd8ade6c9de41a030878d6cc4 Mon Sep 17 00:00:00 2001 From: Rob DiVincenzo Date: Tue, 26 Nov 2024 16:39:01 -0500 Subject: [PATCH] Add fallback checks if link_to is not set (#13180) --- .../customblocks/common/base_link_block.py | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/network-api/networkapi/wagtailpages/pagemodels/customblocks/common/base_link_block.py b/network-api/networkapi/wagtailpages/pagemodels/customblocks/common/base_link_block.py index 13a296e30aa..f91c7143674 100644 --- a/network-api/networkapi/wagtailpages/pagemodels/customblocks/common/base_link_block.py +++ b/network-api/networkapi/wagtailpages/pagemodels/customblocks/common/base_link_block.py @@ -22,12 +22,34 @@ def get_external_url_link(self): def get_relative_url_link(self): return self.get("relative_url") + def get_phone_link(self): + return self.get("phone") + + def get_email_link(self): + return self.get("email") + + def get_file_link(self): + return self.get("file") + + # TODO: Make link_to set on translated pages so no fallback needed @property def url(self): + # If link_to is set, dynamically call the corresponding method link_to = self.get("link_to") - - method = getattr(self, f"get_{link_to}_link") - return method() + if link_to: + method = getattr(self, f"get_{link_to}_link", None) + if method: + return method() + + # Missing link_to field fallback (i.e. translated pages) + # If link_to not found try to find the value + # Since self is in memory, this should be efficient. + for link_type in ["page", "external_url", "relative_url", "phone", "email", "file"]: + method = getattr(self, f"get_{link_type}_link", None) + if method: + result = method() # Call the method + if result: # Return the first valid result + return result def get_link_to(self): """