Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to select the Target in Link Field #16326

Merged
merged 10 commits into from
Jun 16, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public override IDisplayResult Edit(LinkField field, BuildFieldEditorContext con
var settings = context.PartFieldDefinition.GetSettings<LinkFieldSettings>();
model.Url = context.IsNew && field.Url == null ? settings.DefaultUrl : field.Url;
model.Text = context.IsNew && field.Text == null ? settings.DefaultText : field.Text;
model.Target = context.IsNew && field.Target == null ? settings.DefaultTarget : field.Target;

model.Field = field;
model.Part = context.ContentPart;
Expand All @@ -67,7 +68,7 @@ public override IDisplayResult Edit(LinkField field, BuildFieldEditorContext con

public override async Task<IDisplayResult> UpdateAsync(LinkField field, IUpdateModel updater, UpdateFieldEditorContext context)
{
var modelUpdated = await updater.TryUpdateModelAsync(field, Prefix, f => f.Url, f => f.Text);
var modelUpdated = await updater.TryUpdateModelAsync(field, Prefix, f => f.Url, f => f.Text, f => f.Target);

if (modelUpdated)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ public class LinkField : ContentField
public string Url { get; set; }

public string Text { get; set; }

public string Target { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public LinkFieldQueryObjectType()

Field(x => x.Url, nullable: true).Description("the url of the link");
Field(x => x.Text, nullable: true).Description("the text of the link");
Field(x => x.Target, nullable: true).Description("the target of the link");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public override Task BuildIndexAsync(LinkField field, BuildFieldIndexContext con
{
context.DocumentIndex.Set(key, field.Url, options);
context.DocumentIndex.Set(key, field.Text, options);
context.DocumentIndex.Set(key, field.Target, options);
}

return Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class LinkFieldIndex : ContentFieldIndex
public string BigUrl { get; set; }
public string Text { get; set; }
public string BigText { get; set; }
public string Target { get; set; }
}

public class LinkFieldIndexProvider : ContentFieldIndexProvider
Expand Down Expand Up @@ -92,6 +93,7 @@ public override void Describe(DescribeContext<ContentItem> context)
BigUrl = pair.Field.Url,
Text = pair.Field.Text?[..Math.Min(pair.Field.Text.Length, LinkFieldIndex.MaxTextSize)],
BigText = pair.Field.Text,
Target = pair.Field.Target
vengi83644 marked this conversation as resolved.
Show resolved Hide resolved
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class LinkFieldSettings
public string TextPlaceholder { get; set; }
public string DefaultUrl { get; set; }
public string DefaultText { get; set; }
public string DefaultTarget { get; set; }

public LinkFieldSettings()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefiniti
model.TextPlaceholder = settings.TextPlaceholder;
model.DefaultUrl = settings.DefaultUrl;
model.DefaultText = settings.DefaultText;
model.DefaultTarget = settings.DefaultTarget;
}).Location("Content");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class DisplayLinkFieldViewModel
{
public string Url => Field.Url;
public string Text => Field.Text;
public string Target => Field.Target;
public LinkField Field { get; set; }
public ContentPart Part { get; set; }
public ContentPartFieldDefinition PartFieldDefinition { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class EditLinkFieldViewModel
{
public string Url { get; set; }
public string Text { get; set; }
public string Target { get; set; }
public LinkField Field { get; set; }
public ContentPart Part { get; set; }
public ContentPartFieldDefinition PartFieldDefinition { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,27 @@
</div>
}
</div>
<div class="row">
<div class="col-md-6" asp-validation-class-for="Target">
<label asp-for="Target" class="@Orchard.GetLabelClasses()">
@T["Target"]
</label>
<div class="@Orchard.GetEndClasses()">
<input asp-for="Target" list="targetOptions" class="form-control" />
<datalist id="targetOptions">
<option value="_self">@T["Opens the linked document in the same frame as it was clicked (this is default)."]</option>
vengi83644 marked this conversation as resolved.
Show resolved Hide resolved
<option value="_blank">@T["Opens the linked document in a new window or tab."]</option>
<option value="_parent">@T["Opens the linked document in the parent frame."]</option>
<option value="_top">@T["Opens the linked document in the full body of the window."]</option>
</datalist>
<span class="hint">
@T["The target attribute of the A tag, see more:"]
vengi83644 marked this conversation as resolved.
Show resolved Hide resolved
<a class="seedoc" target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#target">
target
vengi83644 marked this conversation as resolved.
Show resolved Hide resolved
</a>
</span>
</div>
</div>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@

href = href.ToUriComponents();
}

var target = !string.IsNullOrWhiteSpace(Model.Target) ? Model.Target : "";
vengi83644 marked this conversation as resolved.
Show resolved Hide resolved
}
<div class="field field-type-linkfield field-name-@name">
<a href="@href">@text</a>
<a href="@href" target="@target">@text</a>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,18 @@
<span asp-validation-for="DefaultText" class="text-danger"></span>
</div>
</div>

<div class="row">
<div class="mb-3 col-md-6">
<label asp-for="DefaultTarget" class="form-label">@T["Default value of the Target"]</label>
<input asp-for="DefaultTarget" list="targetOptions" class="form-control" />
<datalist id="targetOptions">
<option value="_self">@T["Opens the linked document in the same frame as it was clicked (this is default)."]</option>
vengi83644 marked this conversation as resolved.
Show resolved Hide resolved
<option value="_blank">@T["Opens the linked document in a new window or tab."]</option>
<option value="_parent">@T["Opens the linked document in the parent frame."]</option>
<option value="_top">@T["Opens the linked document in the full body of the window."]</option>
</datalist>
<span class="hint">@T["The default target proposed when creating a content item."]</span>
<span asp-validation-for="DefaultTarget" class="text-danger"></span>
</div>
</div>
vengi83644 marked this conversation as resolved.
Show resolved Hide resolved