Skip to content

Commit

Permalink
Add fallback checks if link_to is not set (#13180)
Browse files Browse the repository at this point in the history
  • Loading branch information
robdivincenzo authored Nov 26, 2024
1 parent 7b1ec12 commit af80b08
Showing 1 changed file with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down

0 comments on commit af80b08

Please sign in to comment.