Skip to content

Commit

Permalink
ISSUE-84: Only notify user of Metadata driven title change if change …
Browse files Browse the repository at this point in the history
…is noticeable (#85)

* Only notify user of Metadata driven title change if change is noticeable

basically, don't tell the user everytime it saves a Webform that the title changed if its still the same. Mostly cosmetic, but was driving me insane,.

* Remove message totally

Makes no sense to inform the user of something that is not in their control and should really pass unnoticed.
- Also, for some strange reason the protected variable $loggerFactory was absent in all Event Subscribers and PHPDOC was wrong, no throws here.

* Add missing $loggerFactory

That
  • Loading branch information
DiegoPino authored May 11, 2020
1 parent e4606a9 commit 02ef7a2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class StrawberryfieldEventDeleteFileUsageDeleter extends StrawberryfieldEventDel
*/
protected $strawberryfilepersister;

/**
* The logger factory.
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $loggerFactory;

/**
* StrawberryfieldEventInsertFileUsageUpdater constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class StrawberryfieldEventInsertFileUsageUpdater extends StrawberryfieldEventIns
*/
protected $serializer;

/**
* The logger factory.
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $loggerFactory;

/**
* The Strawberryfield File Persister Service
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class StrawberryfieldEventInsertSubscriberDepositDO extends StrawberryfieldEvent
*/
protected $strawberryfilepersister;

/**
* The logger factory.
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $loggerFactory;

/**
* StrawberryfieldEventInsertSubscriberDepositDO constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class StrawberryfieldEventPresaveSubscriberFilePersister extends Strawberryfield
*/
protected $strawberryfilepersister;

/**
* The logger factory.
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $loggerFactory;

/**
* StrawberryfieldEventPresaveSubscriberFilePersister constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class StrawberryfieldEventPresaveSubscriberSetTitlefromMetadata extends Strawber
*/
protected $destinationScheme = NULL;

/**
* The logger factory.
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $loggerFactory;


/**
* StrawberryfieldEventPresaveSubscriberFilePersister constructor.
*
Expand All @@ -59,20 +66,18 @@ public function __construct(

/**
* @param \Drupal\strawberryfield\Event\StrawberryfieldCrudEvent $event
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function onEntityPresave(StrawberryfieldCrudEvent $event) {

/* @var $entity \Drupal\Core\Entity\ContentEntityBase */
/* @var $entity \Drupal\node\Entity\Node */
$entity = $event->getEntity();
$sbf_fields = $event->getFields();
$titleadded = NULL;
$originallabel = NULL;
$forceupdate = TRUE;
if (!$entity->isNew()) {
// Check if we had a title, if the new one is different and not empty.
if (($entity->original->getTitle() != $entity->label()) && !empty($entity->label())) {
$originallabel = $entity->original->getTitle();
if (($originallabel != $entity->label()) && !empty($entity->label())) {
// Means someone manually, via a Title Widget, changed the title
// If so, enforce that and don't try to overwrite.
// But, webform widget, if updating title automatically is set, should
Expand All @@ -92,6 +97,7 @@ public function onEntityPresave(StrawberryfieldCrudEvent $event) {
// This will try with any possible match.
foreach ($field->getIterator() as $delta => $itemfield) {
$flat = $itemfield->provideFlatten();
// @TODO Should we allow which JSON key sets the label a setting?
if (isset($flat['label'])) {
// Flattener should always give me an array?
if (is_array($flat['label'])) {
Expand All @@ -101,34 +107,28 @@ public function onEntityPresave(StrawberryfieldCrudEvent $event) {
$title = $flat['label'];
}
if (strlen(trim($title)) > 0) {
$title = Unicode::truncate($title,128,TRUE,TRUE, 24);
$title = Unicode::truncate($title, 128, TRUE, TRUE, 24);
// we could check if originallabel != from the new title
// I feel safer assinging and checking only for the status.
$entity->setTitle($title);
$titleadded = $title;
break 2;
}
}
}
}
if ($titleadded) {
$this->messenger->addStatus(
$this->t(
'Your New Object title is @title',
['@title' => $titleadded ]
)

// If at this stage we have no entity label, but maybe its just not in our metadata,
// or someone forget to set a label key.
if (!$entity->label()) {
// Means we need a title, got nothing from metadata or node, dealing with it.
$title = $this->t(
'No New Untitled Archipelago Digital Object by @author',
['@author' => $entity->getOwner()->getDisplayName()]
);
} else {
// But maybe its just not in our metadata or someone forget to set a label key. Don't try to add it if so
if (!$entity->label()) {
// Means we need a title, got nothing from metadata or node, dealing with it.
$title = $this->t(
'New Untitled Archipelago Digital Object by @author',
['@author' => $entity->getOwner()->getDisplayName()]
);
$entity->setTitle($title);
}
$entity->setTitle($title);
}
$current_class = get_called_class();
$event->setProcessedBy($current_class, TRUE);
}
$current_class = get_called_class();
$event->setProcessedBy($current_class, TRUE);
}
}

0 comments on commit 02ef7a2

Please sign in to comment.