Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion name_prepopulate.info.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
name: Name Prepopulate
description: Prepopulates name fields from the user account.
core_version_requirement: ^8 || ^9
core_version_requirement: ^9 || ^10
package: Fields
type: module
dependencies:
- name:name
4 changes: 2 additions & 2 deletions name_prepopulate.module
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use Drupal\name_prepopulate\NamePrepopulateHelper;
* Implements hook_entity_prepare_form().
*/
function name_prepopulate_entity_prepare_form(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Form\FormStateInterface $form_state) {
$nameField = 'field_name';
$nameField = 'name';

$types = ['contact_message'];

Expand All @@ -27,7 +27,7 @@ function name_prepopulate_entity_prepare_form(\Drupal\Core\Entity\EntityInterfac
* Implements hook_entity_presave().
*/
function name_prepopulate_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
$nameField = 'field_name';
$nameField = 'name';

$types = ['contact_message'];

Expand Down
14 changes: 6 additions & 8 deletions src/NamePrepopulateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\name\NameFormatParser;
use Drupal\user\Entity\User;

class NamePrepopulateHelper {
Expand Down Expand Up @@ -51,19 +50,18 @@ public function prepopulateFromAccount() {
}

protected function getNameString() {
/** @var \Drupal\Core\Field\FieldConfigInterface $fieldConfig; */
$fieldConfig = FieldConfig::loadByName($this->entity->getEntityTypeId(), $this->entity->bundle(), $this->fieldName);

/** @var \Drupal\name\NameFormatInterface $format */
$format = \Drupal::entityTypeManager()->getStorage('name_format')->load($fieldConfig->getSetting('override_format'));

$field = $this->getField();

if ($field->isEmpty()) {
return '';
}

return NameFormatParser::parse($field->get(0)->getValue(), $format->get('pattern'), ['object' => $this->entity, 'type' => $this->entity->getEntityTypeId()]);
$value = ($field->get(0)->getValue())['value'];
if ($value instanceof \Drupal\Component\Render\HtmlEscapedText) {
return $value->__toString();
} else {
return $value;
}
}

protected function getField() {
Expand Down