Skip to content

Commit 760eb1f

Browse files
committed
Add custom link target handling override
1 parent a3b255b commit 760eb1f

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# frozen_string_literal: true
2+
3+
# Override last checked at: 04.04.2025
4+
Decidim::ParticipatoryProcesses::Admin::ParticipatoryProcessesController.class_eval do
5+
def edit
6+
enforce_permission_to :update, :process, process: current_participatory_process
7+
8+
@form = form(Decidim::ParticipatoryProcesses::Admin::ParticipatoryProcessForm).from_model(current_participatory_process)
9+
10+
@form[:description_de] = reset_url_target(@form[:description_de])
11+
@form[:description_en] = reset_url_target(@form[:description_en])
12+
13+
render layout: "decidim/admin/participatory_process"
14+
end
15+
16+
private
17+
18+
def participatory_process_params
19+
ppp = { id: params[:slug] }.merge(params[:participatory_process].to_unsafe_h)
20+
21+
ppp['description_de'] = apply_url_target(ppp['description_de'])
22+
ppp['description_en'] = apply_url_target(ppp['description_en'])
23+
24+
ppp
25+
end
26+
27+
def apply_url_target(text)
28+
pattern = %r{<a href="([^"]+?)(:?\?|\&amp;)target=([^"]+?)"(.+?)(:?target="_blank")?>}
29+
matches = matches(text, pattern)
30+
31+
matches
32+
.reduce(text) do |text, match|
33+
href, _, target, params = match.captures.map(&:strip)
34+
35+
text.gsub(match[0], %(<a href="#{href}" #{params} target="#{target}">))
36+
end
37+
end
38+
39+
def reset_url_target(text)
40+
pattern = %r{<a href="(.+?)"(.+?)target="(.+?)">}
41+
matches = matches(text, pattern)
42+
43+
matches
44+
.reject { |match| match[3] == '_blank' }
45+
.reduce(text) do |text, match|
46+
href, params, target = match.captures.map(&:strip)
47+
next if target == '_blank'
48+
49+
divider = href.split('/').last.include?('?') ? '&amp;' : '?'
50+
new_href = "#{href}#{divider}target=#{target}"
51+
52+
text.gsub(match[0], %(<a href="#{new_href}" #{params} target="_blank">))
53+
end
54+
end
55+
56+
def matches(str, regexp)
57+
start_at = 0
58+
matches = []
59+
while(match = str.match(regexp, start_at))
60+
matches.push(match)
61+
start_at = match.end(0)
62+
end
63+
matches
64+
end
65+
end

doc/overrides_patches_hacks.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Hier sammeln wir alle Fixes zusammen mit den Gründen warum wir sie brauchen und
3838
| [layouts/mailer.html.erb](app/views/layouts/mailer.html.erb) | ? | ? | ? | - |
3939
| [decidim_zuerich_customization.rb](config/initializers/decidim_zuerich_customization.rb) | ? | ? | ? | - |
4040
| [resource_locator_presenter.rb](app/presenters/decidim_zuerich/resource_locator_presenter.rb) | Fix für Notifications | ? | ? | - |
41+
| [participatory_processes_controller.rb](app/overrides/controllers/decidim/participatory_processes/admin/participatory_processes_controller_override.rb) | Custom link target handling | ? | ? | - |
4142
# Archivierte Fixes
4243
| Grund | Commit | Anmerkungen |
4344
| ----- | ------ | ----------- |

0 commit comments

Comments
 (0)