-
-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Daterange handler #2 #167
Open
jonathanjfshaw
wants to merge
1
commit into
jhedstrom:master
Choose a base branch
from
shrimala:daterange2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Daterange handler #2 #167
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Drupal\Driver\Fields\Drupal8; | ||
|
||
/** | ||
* Daterange field handler for Drupal 8. | ||
*/ | ||
class DaterangeHandler extends AbstractHandler { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function expand($values) { | ||
$return = []; | ||
foreach ($values as $value) { | ||
// Allow date ranges properties to be specified either explicitly, | ||
// or implicitly by array position. | ||
if (!isset($value['value']) && isset($value[0])) { | ||
$value['value'] = $value[0]; | ||
} | ||
if (!isset($value['end_value'])) { | ||
if (isset($value[1])) { | ||
$value['end_value'] = $value[1]; | ||
} | ||
else { | ||
// Allow end value to be optional (D#2794481). | ||
$value['end_value'] = NULL; | ||
} | ||
} | ||
$start = str_replace(' ', 'T', $value['value']); | ||
$end = str_replace(' ', 'T', $value['end_value']); | ||
$return[] = [ | ||
'value' => $start, | ||
'end_value' => $end, | ||
]; | ||
} | ||
return $return; | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that this will work as expected in all use cases. For example this wouldn't work with time formats like "December 1, 1987" or "now +1 hour".
I'm not sure if it is even the scope of DrupalDriver to "fix" bad input. My gut feeling says that if the time format is not in the expected format we should throw an exception here. It seems more like the responsibility of the API consumer (e.g. DrupalExtension) to make sure the data is in the correct format.
But then again, DrupalDriver can realistically be expected to be able to deal with all kinds of date formats that are used by Drupal versions 6-8 (and future versions). So maybe it is a good idea to support multiple time formats here.
How about something like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's a problem with the D8 datetime handler too;
relative datetime support was recently added to the D8 handler; I didn't add it to the daterange handler as I was being quick and dirty.
Yes, there are fundamental questions about how liberal the driver should be with its inputs. It comes up with the entity reference handler where we allow reference by labels, and it comes up with the question of whether we should allow specifying labels as well as machine names in some other contexts.
The best context for discussing that is #155, the new plugin system. We need to decide if the Drupal driver provides basic plugins that are extended and overridden by more helpful human-friendly plugins from the Drupal extension, or if its just simpler to have only one set of human-friendly plugins that live in in the driver.
My suggestion is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about extending
DateTimeHandler
so that the relative date handling is shared?E.g something like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've given that approach a quick test ericgsmith@f59eecb and works for me setting relative dates.
Appreciate this is a old MR - but given the suggested scope didn't get merge, would be keen to get feedback on that approach.