Created
January 23, 2024 13:37
-
-
Save welly/bddd9318ec7396d768c58073b44128ce to your computer and use it in GitHub Desktop.
Adding a custom property to a Config Entity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\paragraphs\Entity\ParagraphsType; | |
/** | |
* Implements hook_form_alter(). | |
*/ | |
function example_form_form_alter(&$form, FormStateInterface $form_state, $form_id) { | |
$form_ids = [ | |
'paragraphs_type_add_form', | |
'paragraphs_type_edit_form', | |
]; | |
if (in_array($form_id, $form_ids)) { | |
$attribute = $form_state->getFormObject()->getEntity(); | |
$form['custom_property'] = [ | |
'#type' => 'textfield', | |
'#title' => 'Custom property', | |
'#default_value' => $attribute->getThirdPartySetting('example_form', 'custom_property'), | |
]; | |
$form['#entity_builders'][] = 'example_form_custom_property_form_builder'; | |
} | |
} | |
/** | |
* Entity builder for the paragraphs_type config entity. | |
*/ | |
function example_form_custom_property_form_builder($entity_type, ParagraphsType $custom_property, &$form, FormStateInterface $form_state) { | |
if ($form_state->getValue('custom_property')) { | |
$custom_property->setThirdPartySetting('example_form', 'custom_property', $form_state->getValue('custom_property')); | |
return; | |
} | |
$custom_property->unsetThirdPartySetting('example_form', 'custom_property'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
paragraphs.paragraphs_type.*.third_party.example_form: | |
type: config_entity | |
label: 'Custom property' | |
mapping: | |
custom_property: | |
type: string | |
label: 'Custom property' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment