-
-
Save tristanbes/d3c74c12d60e20df84c3 to your computer and use it in GitHub Desktop.
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
<?php | |
namespace MyNamespace\TechnicalFactBundle\Form\Type; | |
use Symfony\Component\Form\Extension\Core\Type\DateType; | |
use Symfony\Component\Form\FormInterface; | |
use Symfony\Component\Form\FormViewInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
/** | |
* Class Jquery data type | |
*/ | |
class jQueryDateType extends DateType | |
{ | |
/** | |
* Gets the name | |
* | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return 'jquery_date'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function finishView(FormViewInterface $view, FormInterface $form, array $options) | |
{ | |
$view->setVar('widget', $options['widget']); | |
// Remove date type, so that chrome does not show up its own date picker | |
/* | |
if ('single_text' === $options['widget']) { | |
$view->setVar('type', 'date'); | |
} | |
*/ | |
if ($form->getConfig()->hasAttribute('formatter')) { | |
$pattern = $form->getConfig()->getAttribute('formatter')->getPattern(); | |
$format = $pattern; | |
$format = strtr($format, array('MMM' => 'M', 'yyyy' => 'yy')); | |
$view->setVar('locale', \Locale::getDefault()); | |
$view->setVar('date_format', $format); | |
if (count($view) > 0) { | |
$pattern = $form->getConfig()->getAttribute('formatter')->getPattern(); | |
$format = $pattern; | |
// set right order with respect to locale (e.g.: de_DE=dd.MM.yy; en_US=M/d/yy) | |
// lookup various formats at http://userguide.icu-project.org/formatparse/datetime | |
if (preg_match('/^([yMd]+).+([yMd]+).+([yMd]+)$/', $pattern)) { | |
$pattern = preg_replace(array('/y+/', '/M+/', '/d+/'), array('{{ year }}', '{{ month }}', '{{ day }}'), $pattern); | |
} else { | |
// default fallback | |
$pattern = '{{ year }}-{{ month }}-{{ day }}'; | |
} | |
$view->setVar('date_pattern', $pattern); | |
} | |
} | |
} | |
/** | |
* Set default options | |
* | |
* @param OptionsResolverInterface $resolver | |
*/ | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
parent::setDefaultOptions($resolver); | |
$resolver->setDefaults(array( | |
'widget' => 'single_text' | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment