diff --git a/assets/js/editing-helper.js b/assets/js/editing-helper.js index fe3ecf06ad1c19ebaa6b7415652007196de9b16b..79231cf227d423f7a868bdfec3408831dbe98722 100644 --- a/assets/js/editing-helper.js +++ b/assets/js/editing-helper.js @@ -41,8 +41,6 @@ // Mark the element as having the event listener attached parentElement.setAttribute("data-listener-attached", "true"); } - } else { - console.error("Help content not found within the parent element!"); } }); }, diff --git a/config/schema/editing_helper.schema.yml b/config/schema/editing_helper.schema.yml new file mode 100755 index 0000000000000000000000000000000000000000..b3a9ff832a0e515752fcc9788a53bc9f06e1284e --- /dev/null +++ b/config/schema/editing_helper.schema.yml @@ -0,0 +1,7 @@ +field.field.*.*.*.third_party.editing_helper: + type: mapping + label: "Field settings for tablefield_required" + mapping: + editing_helper_description: + type: string + label: "Editing Helper Description" diff --git a/editing_helper.module b/editing_helper.module index 2ff31a5a7d53437420256c346e0e49c2c8a67a5f..862b6a2338e1cddb6de6f857655bbf401fca1057 100644 --- a/editing_helper.module +++ b/editing_helper.module @@ -6,7 +6,9 @@ */ use Drupal\block_content\BlockContentInterface; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\field\Entity\FieldConfig; /** * Implements hook_help(). @@ -20,6 +22,46 @@ function editing_helper_help($route_name, RouteMatchInterface $route_match) { } } +/** + * Implements hook_form_FORM_ID_alter(). + */ +function editing_helper_form_field_config_edit_form_alter(array &$form, FormStateInterface $form_state) { + /** @var \Drupal\field\Entity\FieldConfig $field_config */ + $field_config = $form_state->getFormObject()->getEntity(); + + $editing_helper_description = $field_config->getThirdPartySettings('editing_helper'); + + $form['editing_helper_description'] = [ + '#type' => 'textarea', + '#title' => t('Editing Helper Description.'), + '#default_value' => $editing_helper_description["editing_helper_description"] ?? '', + '#description' => t('Instructions to present to the user below this field on the editing form. + Allowed HTML tags'), + ]; + + $form['#entity_builders'][] = 'editing_helper_form_builder'; +} + +/** + * Update the field configuration once form is saved. + * + * @param \Drupal\field\Entity\FieldConfig $config + * The field configuration entity. + * @param array $form + * The complete form array. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + */ +function editing_helper_form_builder(FieldConfig $config, array &$form, FormStateInterface $form_state) { + + $editing_helper_description = $form_state->getValue('editing_helper_description'); + if ($editing_helper_description) { + $config->setThirdPartySetting('editing_helper', 'editing_helper_description', $editing_helper_description); + return; + } + $config->unsetThirdPartySetting('editing_helper', 'editing_helper_description'); +} + /** * Implements hook_preprocess_block(). */ @@ -34,8 +76,7 @@ function editing_helper_preprocess_block(array &$variables) { // Check if the block is a field or views block. if ($plugin_id) { - [$block_type, $block_info] = explode(':', $plugin_id) + [NULL, NULL]; - + [$block_type, $block_info, $block_bundle, $block_field] = explode(':', $plugin_id) + [NULL, NULL, NULL, NULL]; switch ($block_type) { case 'block_content': $variables["attributes"]["class"][] = "editing-helper-block editing-helper-block-content"; @@ -47,8 +88,13 @@ function editing_helper_preprocess_block(array &$variables) { break; case 'field_block': - $variables["attributes"]["class"][] = "editing-helper-block editing-helper-field"; - $help_text = $config->get('view_field_text'); + $field_config = FieldConfig::loadByName($block_info, $block_bundle, $block_field); + if (!is_null($field_config)) { + $editing_helper_description = $field_config->getThirdPartySettings('editing_helper'); + + $variables["attributes"]["class"][] = "editing-helper-block editing-helper-field"; + $help_text = !empty($editing_helper_description) ? $editing_helper_description["editing_helper_description"] : $config->get('view_field_text'); + } break; case 'views_block': @@ -60,6 +106,16 @@ function editing_helper_preprocess_block(array &$variables) { // Get the current display. $display = $view->getDisplay($view_display); + switch ($view->get('base_field')) { + case 'nid': + $help_default_text = $config->get('view_node_text'); + break; + + case 'tid': + $help_default_text = $config->get('view_taxonomy_text'); + break; + } + $extenders = $display["display_options"]["display_extenders"]["editing_helper_display_extender"]; if (!isset($extenders)) { return; @@ -67,7 +123,7 @@ function editing_helper_preprocess_block(array &$variables) { // Retrieve the settings of our plugins using our custom plugin method. $description_values = $extenders["view_description_helper"]["description"]; - $help_text = $description_values; + $help_text = !empty($description_values) ? $description_values : $help_default_text; } break; } @@ -75,6 +131,7 @@ function editing_helper_preprocess_block(array &$variables) { // Add the help wrapper with the computed help text. if (!empty($help_text) && $current_user->hasPermission('access to editing helper')) { + $block_title = (string) $config->get('block_title'); $variables['title_prefix']['help_wrapper'] = [ '#type' => 'container', '#attributes' => ['class' => ['help-wrapper']], @@ -82,7 +139,7 @@ function editing_helper_preprocess_block(array &$variables) { 'toggle_help_button' => [ '#type' => 'html_tag', '#tag' => 'button', - '#value' => t('Want Help!'), + '#value' => $block_title ? $block_title : t('Want Help!'), '#attributes' => [ 'class' => ['toggle-help'], 'type' => 'button', diff --git a/src/Form/HelpDescriptionConfigForm.php b/src/Form/HelpDescriptionConfigForm.php index 2583fb0dba6fd4ea4ec1db2717d9b520d2c274e7..de00ae2dfeeee5c0728030bb2d816ace05a786f8 100644 --- a/src/Form/HelpDescriptionConfigForm.php +++ b/src/Form/HelpDescriptionConfigForm.php @@ -49,6 +49,12 @@ class HelpDescriptionConfigForm extends ConfigFormBase { '#tree' => FALSE, ]; + $form['help_config']['block_title'] = [ + '#type' => 'textfield', + '#title' => $this->t('Block Title'), + '#default_value' => $config->get('block_title'), + ]; + $form['help_config']['block_inline_text'] = [ '#type' => 'textarea', '#title' => $this->t('Block Inline Text'), @@ -64,6 +70,17 @@ class HelpDescriptionConfigForm extends ConfigFormBase { '#title' => $this->t('View Field Text'), '#default_value' => $config->get('view_field_text'), ]; + $form['help_config']['view_node_text'] = [ + '#type' => 'textarea', + '#title' => $this->t('View Node Text'), + '#default_value' => $config->get('view_node_text'), + ]; + $form['help_config']['view_taxonomy_text'] = [ + '#type' => 'textarea', + '#title' => $this->t('View Taxonomy Term Text'), + '#default_value' => $config->get('view_taxonomy_text'), + ]; + return parent::buildForm($form, $form_state); } @@ -79,9 +96,12 @@ class HelpDescriptionConfigForm extends ConfigFormBase { */ public function submitForm(array &$form, FormStateInterface $form_state) { $this->config('editing_helper.help_config') + ->set('block_title', $form_state->getValue('block_title')) ->set('block_inline_text', $form_state->getValue('block_inline_text')) ->set('block_reusable_text', $form_state->getValue('block_reusable_text')) ->set('view_field_text', $form_state->getValue('view_field_text')) + ->set('view_node_text', $form_state->getValue('view_node_text')) + ->set('view_taxonomy_text', $form_state->getValue('view_taxonomy_text')) ->save(); $this->messenger()->addStatus($this->t("Seeds Layout configuration has been saved successfully")); }