dvModelPageData function
DVPageData
dvModelPageData(
- DVModelPageSpec spec,
- Map<
String, Object?> row, { - String? applicationFavicon,
The page data a model row is: the title field, the longest content field as description and text, the image, and structured data naming the record; hidden when the published field says so.
Implementation
DVPageData dvModelPageData(
DVModelPageSpec spec,
Map<String, Object?> row, {
String? applicationFavicon,
}) {
// A person's row, or one that is not published, is answered exactly as a
// row that is not there: the same data, so nothing about the response says
// which of the three it was.
if (spec.personal) return dvModelPageNotFound(spec);
final Object? published = spec.publishedField == null ? true : row[spec.publishedField];
final bool visible = published == true || published == 1 || '$published'.toLowerCase() == 'true';
if (!visible) return dvModelPageNotFound(spec);
// Read through this rather than the row, so a protected field cannot be
// reached from any of the places below whatever the spec names.
String? text(String? field) {
if (field == null || spec.protectedFields.contains(field)) return null;
final Object? value = row[field];
if (value == null) return null;
final String s = '$value'.trim();
return s.isEmpty ? null : s;
}
final String title = text(spec.titleField) ?? text(spec.keyField) ?? spec.model;
String? content;
for (final String field in spec.contentFields) {
final String? candidate = text(field);
if (candidate != null && (content == null || candidate.length > content.length)) content = candidate;
}
final String? image = text(spec.imageField);
return DVPageData(
title: title,
description: content,
image: image,
// Never the featured image. This field was declared, emitted by the head
// writer, and set by nothing at all, so every model page wore the
// shell's icon -- and filling it with the featured image would have been
// worse than leaving it: a photograph at 32 pixels is the whole
// photograph, downloaded on every page.
favicon: spec.favicon ?? applicationFavicon,
text: <String>[title, if (content != null && content != title) content],
structuredData: <String, Object?>{
'@context': 'https://schema.org',
'@type': spec.schemaType ?? 'Thing',
'name': title,
if (content != null) 'description': content,
if (image != null) 'image': image,
},
visibility: DVPageVisibility.public,
);
}