Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions src/Entity/AiSocialPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,28 +92,39 @@ public static function preCreate(
return;
}

// Get the field definition for the 'post' field.
// Get field definitions for all fields.
$field_manager = \Drupal::service('entity_field.manager');
$fields = $field_manager->getFieldDefinitions(
'ai_social_post',
$values['type']
);

// Only process the 'post' field if it exists and has a default value.
if (isset($fields['post']) && $fields['post'] instanceof FieldConfig) {
$default = $fields['post']->getDefaultValueLiteral();
if (!empty($default[0]['value'])) {
// Preserve original field settings.
$values['post'] = $default[0];

// Add URL and prompt to the 'post' field.
$values['post']['value'] = sprintf(
'/%s',
t('For :url @prompt. Include the link.', [
':url' => $url,
'@prompt' => ltrim($default[0]['value'], '/ '),
], ['context' => 'Social post with URL'])
);
// Define content fields that should get the "For [url]" prefix.
$content_field_names = ['post', 'title', 'subtitle'];
$content_field_types = ['text_long', 'string', 'string_long'];

// Process each content field that has a default value.
foreach ($content_field_names as $field_name) {
if (isset($fields[$field_name]) && $fields[$field_name] instanceof FieldConfig) {
// Check if it's a content field type.
if (!in_array($fields[$field_name]->getType(), $content_field_types)) {
continue;
}

$default = $fields[$field_name]->getDefaultValueLiteral();
if (!empty($default[0]['value'])) {
// Preserve original field settings.
$values[$field_name] = $default[0];

// Add URL and prompt to the field.
$values[$field_name]['value'] = sprintf(
'/%s',
t('For :url @prompt. Include the link.', [
':url' => $url,
'@prompt' => ltrim($default[0]['value'], '/ '),
], ['context' => 'Social post with URL'])
);
}
}
}
}
Expand Down