Skip to content

Commit

Permalink
Add 'Add ' string/text to entity reference add button
Browse files Browse the repository at this point in the history
  • Loading branch information
golddragon007 committed Nov 22, 2017
1 parent 6c2fe58 commit dce888f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ content:
add_mode: dropdown
form_display_mode: default
default_paragraph_type: ''
add_text_needed: '1'
third_party_settings: { }
region: content
parade_text:
Expand Down
10 changes: 10 additions & 0 deletions parade.install
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,13 @@ function parade_update_8203() {
}
}
}

/**
* Add new option (add_text_needed) default value.
*/
function parade_update_8204() {
$config_factory = \Drupal::configFactory();
$config_factory->getEditable('core.entity_form_display.paragraph.text_boxes.default')
->set('content.parade_paragraphs.settings.add_text_needed', '1')
->save();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\paragraphs\Plugin\Field\FieldWidget\InlineParagraphsWidget;
use Drupal\parade\Plugin\Field\FieldWidget\InlineParagraphsWidget;

/**
* Plugin implementation of the 'Paragraphs with preview' widget.
Expand Down Expand Up @@ -158,8 +158,11 @@ public function formMultipleElements(FieldItemListInterface $items, array &$form
*/
protected function buildButtonsAddMode() {
$add_more_elements = parent::buildButtonsAddMode();
foreach ($this->getAccessibleOptions() as $machine_name => $label) {
$add_more_elements['add_more_button_' . $machine_name]['#value'] = $label;
// Do not run this, if we need the 'Add ' string.
if (!$this->getSetting('add_text_needed')) {
foreach ($this->getAccessibleOptions() as $machine_name => $label) {
$add_more_elements['add_more_button_' . $machine_name]['#value'] = $label;
}
}
$add_more_elements['#theme_wrappers'] = ['parade__paragraphs_dropbutton_wrapper'];
$add_more_elements['#label'] = [
Expand Down
64 changes: 64 additions & 0 deletions src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Drupal\parade\Plugin\Field\FieldWidget;

use Drupal\Core\Form\FormStateInterface;

/**
* Plugin implementation of the 'entity_reference parade' widget.
*
* We hide add / remove buttons when translating to avoid accidental loss of
* data because these actions effect all languages.
*
* @FieldWidget(
* id = "entity_reference_parade",
* label = @Translation("Parade Classic"),
* description = @Translation("A parade inline form widget."),
* field_types = {
* "entity_reference_revisions"
* }
* )
*/
class InlineParagraphsWidget extends \Drupal\paragraphs\Plugin\Field\FieldWidget\InlineParagraphsWidget {

/**
* {@inheritdoc}
*/
public static function defaultSettings() {
$settings = parent::defaultSettings();

// New values.
$settings['add_text_needed'] = FALSE;

return $settings;
}

/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);

// New values.
$elements['add_text_needed'] = array(
'#type' => 'checkbox',
'#title' => $this->t("Add 'Add ' text before title on entity add buttons."),
'#default_value' => $this->getSetting('add_text_needed'),
'#weight' => 2,
);

return $elements;
}

/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();

// New values.
$summary[] = $this->t("'Add ' text will be added before title?: @answer", ['@answer' => $this->getSetting('add_text_needed') ? 'Yes' : 'No']);

return $summary;
}
}

0 comments on commit dce888f

Please sign in to comment.