reverted: --- b/config/schema/social_profile_field.schema.yml +++ /dev/null @@ -1,20 +0,0 @@ -# Schema for the configuration of the Social profile field type. - -field.storage_settings.social_profile_url: - type: mapping - label: 'Social profile settings' - -field.field_settings.social_profile_url: - type: mapping - label: 'Social profile settings' - sequence: - - type: string - label: 'Setting' - -field.value.social_profile_url: - type: mapping - label: 'Default value' - mapping: - value: - type: url - label: 'Value' diff -u b/js/social_profile_field.js b/js/social_profile_field.js --- b/js/social_profile_field.js +++ b/js/social_profile_field.js @@ -10,7 +10,7 @@ "use strict"; Drupal.behaviors.socialProfileField = { attach: function (context) { - $(document, context).on("change" ,".edit-field-social-profile-url" , function (event) { + $(context).on("change" ,".edit-field-social-profile-url" , function (event) { if ($(this).val()) { var url = $.url($(this).val()); var domain = url.attr('host').replace('.', '-').replace('.', '-'); @@ -25,8 +25,7 @@ } } }); - - $(".edit-field-social-profile-url", context).change(); + $(context).find(".edit-field-social-profile-url").change(); } }; })(jQuery, Drupal, drupalSettings); diff -u b/social_profile_field.info.yml b/social_profile_field.info.yml --- b/social_profile_field.info.yml +++ b/social_profile_field.info.yml @@ -6,2 +5,0 @@ -dependencies: - - field diff -u b/social_profile_field.libraries.yml b/social_profile_field.libraries.yml --- b/social_profile_field.libraries.yml +++ b/social_profile_field.libraries.yml @@ -10,9 +10,4 @@ - dependencies: - - core/jquery - - core/drupalSettings -social_profile_field.urlparser: - version: VERSION - js: js/jquery.urlparser/jquery.url.js: {} dependencies: - core/jquery + - core/drupalSettings diff -u b/social_profile_field.module b/social_profile_field.module --- b/social_profile_field.module +++ b/social_profile_field.module @@ -4,33 +4,3 @@ * @file - * Social profile field using the Field Types API. - */ - -/** - * @defgroup social_profile_field - * @ingroup social - * @{ - * - * Direct port module from 7.x version. - * - * Providing a field requires: - * - Defining an entity field item. Entity field items are typed data objects - * containing the field values. - * - Drupal\social_profile_field\Plugin\Field\FieldType\SocialProfileFieldItem - * - Defining a field type schema at config/schema/[module_name].schema.yml - * - config/schema/social_profile_field.schema.yml - * - Defining a widget for the edit form - * - Drupal\social_profile_field\Plugin\Field\FieldWidget\SocialProfileFieldDefaultWidget - * - Defining a formatter for the field (the portion that outputs the field for - * display) - * - Drupal\social_profile_field\Plugin\field\formatter\SocialProfileFieldIconsFormatter - * - Drupal\social_profile_field\Plugin\field\formatter\SocialProfileFieldListFormatter - * - * @todo - * Port module description from 7.x version after module devpart porting. - * - * @see field_types - * @see field - * @see schemaapi - * - * @} End of "defgroup social". + * Define Social profile field type. */ reverted: --- b/src/Plugin/Field/FieldFormatter/SocialProfileFieldIconsFormatter.php +++ /dev/null @@ -1,100 +0,0 @@ - $item) { - $class = array('social-link'); - $domain = $this->_social_profile_field_get_domain($item->value); - if ($domain == 'www.youtube.com' || $domain == 'youtube.com') { - $class[] = 'fancybox-video'; - } - $elements[$delta] = array( - '#markup' => \Drupal::l( - $this->_social_profile_field_get_icon($domain), - Url::fromUri( - trim($item->value), array( - 'html' => TRUE, - 'attributes' => array( - 'class' => $class, - 'target' => '_blank' - ) - ) - ) - ), - '#attached' => array( - 'library' => array( - 'social_profile_field/social_profile_field.css' - ), - ), - ); - } - - return $elements; - } - - /** - * Returns domain name (with subdomains) for given URL. - * @url string URL. - * @result string Domain name. - */ - public function _social_profile_field_get_domain($url) { - return parse_url(check_url(trim($url)), PHP_URL_HOST); - } - - /** - * Returns path to icon file. - */ - public function _social_profile_field_get_icon_path($domain = 'default') { - return drupal_get_path('module', 'social_profile_field') . '/icons/' . $domain . '.png'; - } - - /** - * Returns path to social network icon. - * @url string URL. - * @return string Rendered HTML-code for icon.. - */ - public function _social_profile_field_get_icon($domain) { - $path = $this->_social_profile_field_get_icon_path($domain); - if (!file_exists($path)) { - $path = $this->_social_profile_field_get_icon_path(); - } - return array( - '#theme' => 'image', - '#uri' => $path, - '#attributes' => array( - 'class' => array('social-profile-icon') - ), - ); - } - -} reverted: --- b/src/Plugin/Field/FieldFormatter/SocialProfileFieldListFormatter.php +++ /dev/null @@ -1,49 +0,0 @@ - $item) { - $elements[$delta] = array( - '#markup' => \Drupal::l( - $item->value, - Url::fromUri( - $item->value, - array('attributes' => array('class' => array('social-link'))) - ) - ), - ); - } - return $elements; - } - -} diff -u b/src/Plugin/Field/FieldType/SocialProfileFieldItem.php b/src/Plugin/Field/FieldType/SocialProfileFieldItem.php --- b/src/Plugin/Field/FieldType/SocialProfileFieldItem.php +++ b/src/Plugin/Field/FieldType/SocialProfileFieldItem.php @@ -7,8 +7,8 @@ namespace Drupal\social_profile_field\Plugin\Field\FieldType; -use Drupal\Core\Field\FieldItemBase; use Drupal\Core\Field\FieldStorageDefinitionInterface; +use Drupal\Core\Field\Plugin\Field\FieldType\UriItem; use Drupal\Core\TypedData\DataDefinition; /** @@ -20,33 +20,10 @@ * module = "SocialProfileField", * description = @Translation("Handle social profiles links."), * default_widget = "social_profile_field_default", - * default_formatter = "social_profile_field_icons" + * default_formatter = "social_profile_field_default" * ) */ -class SocialProfileFieldItem extends FieldItemBase { - /** - * {@inheritdoc} - */ - public static function schema(FieldStorageDefinitionInterface $field_definition) { - return array( - 'columns' => array( - 'value' => array( - 'description' => 'Url to social profile.', - 'type' => 'varchar', - 'length' => 255, - ), - ), - ); - } - - /** - * {@inheritdoc} - */ - public function isEmpty() { - $value = $this->get('value')->getValue(); - return $value === ''; - } - +class SocialProfileFieldItem extends UriItem { /** * {@inheritdoc} */ diff -u b/src/Plugin/Field/FieldWidget/SocialProfileFieldDefaultWidget.php b/src/Plugin/Field/FieldWidget/SocialProfileFieldDefaultWidget.php --- b/src/Plugin/Field/FieldWidget/SocialProfileFieldDefaultWidget.php +++ b/src/Plugin/Field/FieldWidget/SocialProfileFieldDefaultWidget.php @@ -7,10 +7,9 @@ namespace Drupal\social_profile_field\Plugin\Field\FieldWidget; +use Drupal\Core\Field\Plugin\Field\FieldWidget\UriWidget; use Drupal\Core\Field\FieldItemListInterface; -use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\Component\Utility\UrlHelper; /** * Plugin implementation of the 'social_profile_field_default' widget. @@ -24,41 +23,32 @@ * ) */ -class SocialProfileFieldDefaultWidget extends WidgetBase { +class SocialProfileFieldDefaultWidget extends UriWidget { /** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { + $element = parent::formElement($items, $delta, $element, $form, $form_state); $element['value'] = $element + array( - '#type' => 'url', - '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL, - '#attributes' => array('class' => array('edit-field-social-profile-url')), - '#placeholder' => $this->t('Put your social profile url'), - '#element_validate' => array(array(get_called_class(), 'validateUriElement')), - // Add javascript to show social network icon on the fly: - '#attached' => array( - 'library' => array( - 'social_profile_field/social_profile_field.css', - 'social_profile_field/social_profile_field.js', - 'social_profile_field/social_profile_field.urlparser', - ), - ), ); + '#attached' => array( + 'library' => array( + 'social_profile_field/social_profile_field.css', + 'social_profile_field/social_profile_field.js', + ), + ), + '#attributes' => array( + 'class' => array('class') + ) + ); + return $element; } /** - * Disallows saving inaccessible or untrusted URLs. - * + * {@inheritdoc} */ - public static function validateUriElement($element, FormStateInterface $form_state, $form) { - $uri = $element['#value']; - - if (!empty($uri) && !in_array(parse_url($uri, PHP_URL_SCHEME), UrlHelper::getAllowedProtocols())) { - $form_state->setError( - $element, - t("URL '@link_path' is invalid. Please check it and provide a valid URL.", - array('@link_path' => $uri) - ) - ); - } + public static function defaultSettings() { + return array( + 'placeholder' => 'Put your social profile url', + ) + parent::defaultSettings(); } } only in patch2: unchanged: --- /dev/null +++ b/src/Plugin/Field/FieldFormatter/SocialProfileFieldDefaultFormatter.php @@ -0,0 +1,98 @@ + 0, + ) + parent::defaultSettings(); + } + + /** + * {@inheritdoc} + */ + public function viewElements(FieldItemListInterface $items) { + $elements = parent::viewElements($items); + $path_to_icon = drupal_get_path('module', 'social_profile_field') . '/icons/'; + $show_icons = $this->getSetting('icons_show'); + + foreach ($elements as $delta => $element) { + if ($show_icons) { + $icon_path = $path_to_icon . 'default.png'; + $domain_icon = $path_to_icon . parse_url(check_url(trim($items[$delta]->value)), PHP_URL_HOST) . '.png'; + if (file_exists($domain_icon)) { + $icon_path = $domain_icon; + } + + $elements[$delta]['#title'] = array( + '#theme' => 'image', + '#uri' => $icon_path, + '#attributes' => array( + 'class' => array('social-profile-icon') + ), + ); + + $elements[$delta]['#attached'] = array( + 'library' => array( + 'social_profile_field/social_profile_field.css', + ), + ); + } + + $elements[$delta]['#attributes'] = array('class' => array('social-link')); + } + + return $elements; + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, FormStateInterface $form_state) { + $element = array(); + $element['icons_show'] = array( + '#type' => 'checkbox', + '#title' => $this->t('Show icons instead urls list.'), + '#default_value' => $this->getSetting('icons_show'), + '#description' => $this->t("Outputs Social Network icons linked to user's profiles."), + ); + return $element; + } + + /** + * {@inheritdoc} + */ + public function settingsSummary() { + return array($this->t('Show Icons instead urls list: @icons_show', array( + '@icons_show' => $this->getSetting('icons_show') ? 'Yes' : 'No', + ))); + return array(); + } +}