Skip to main content

GSoC'19 Coding Period | Week #11 | Icon API


The Google Summer of Code has reached the end of the week #11 of the coding period with my project "Complete porting of Icons API to Drupal 8" being progressing smoothly with interesting challenges along the way.

In my previous blog post, I had mentioned that I'd be fixing the autocomplete issue with the Icon Field module and looking for any bugs in the whole project.

During this week, I've implemented the autocomplete update thing in the Icon Field module and the following code is called when there is a change in the select field using the ajax:
/**
 *
 * Returns the 'Search icon' textfield
 */
public function updateIconField(array &$form, FormStateInterface $form_state) {
    // Get the delta
    $delta = $form['#icon_field_delta'];

    // Get the field name
    $field_name = $form['#icon_field_name'];

    $icon_form = $form[$field_name]['widget']['' . $delta . '']['icon'];

    // Get the current icon bundle key
    $value = $form_state->getTriggeringElement()['#value'];

    // Set the autocomplete route name to the current icon bundle key
    $icon_form['#autocomplete_route_name'] = $value;

    // Set the default value
    $icon_form['#value'] = '';

    // Set the autocomplete atttribute
    $icon_form['#attributes']['data-autocomplete-path'] = Url::fromRoute($value)->toString();

    return $icon_form;
  }
The following is the code of the Icon Bundle select field and Search Icon textfield along with the $field_name and $form_field_id variables.
// Get the field name
$field_name = $items->getFieldDefinition()->getName();

$form['#icon_field_name'] = $field_name;
$form['#icon_field_delta'] = $delta;

$form_field_id = 'icon_field-field-wrapper-' . $field_name . '-' . $delta;

// ............

$element['bundle'] = [
  '#title' => $this->t('Icon Bundle'),
  '#type' => 'select',
  '#description' => t('Choose the icon bundle to display the icons using the autocomplete.'),
  '#default_value' => key($icon_bundle), //$items[$delta]->get('bundle')->getValue(),
  '#options' => $icon_bundle,
  '#ajax' => [
    'callback' => '\Drupal\icon_field\Plugin\Field\FieldWidget\IconFieldWidget::updateIconField',
    'event' => 'change',
    'wrapper' => $form_field_id,
  ],
];

$element['icon'] = [
  '#title' => $this->t('Search Icon'),
  '#type' => 'textfield',
  '#prefix' => '<div id="' . $form_field_id . '">',
  '#suffix' => '</div>',
  '#default_value' => $items[$delta]->get('icon')->getValue(),
  '#autocomplete_route_name' => $form_state->getValue('bundle') ? $form_state->getValue('bundle') : key($icon_bundle),
];

// ........
The above solution is working fine but when there are multiple fields it is not updating the intended Search Icon textfield's autocomplete and I've tried various ways to make it work for the multiple field and finally my mentor Chiranjeeb Mahanta suggested to just go with the plain textfield. The select field for the Icon Bundle is just for the User Experience(UX) which allows users to select the specific bundle and search for the icons in that specific bundle and it won't make any difference in the functionality if the textfield is used. The following screenshot shows the Icon Field widget with the Icon Bundle and Icon name as textfields:

The project "Complete porting of Icons API to Drupal 8" is successfully ported along with new features added which were not present in the Drupal 7 version and everything is implemented as proposed in the project proposal and what was discussed with my mentor.

The challenges faced during this week are implementing the update of the Search Icons textfields #autocomplete_route_name when there is a change in the Icon Bundle select field.

For the next week, I'll be working on finding the potential bugs in the whole project and changing the fa class in the HTML tags class with the specific icon style class name and also displaying messages in the admin panel when there is a configuration error.

Comments

Popular posts from this blog

GSoC'19 Coding Period | Week #9 | Icon API

The Google Summer of Code has reached the end of the week #9 of the coding period with my project " Complete porting of Icons API to Drupal 8 " being progressing smoothly with interesting challenges along the way. In my previous blog post , I had mentioned that I'll be implementing the Icon Field sub-module using the Field API with the Field Type, Field Widget and Field Formatter which will allow the users to specify the icon for the content type. During this week, I've implemented the Field Type, Field Widget and Field Formatter using the Plugin API and Annotations. The annotation for the Field Type which resides in the src\Plugin\Field\FieldType\IconFieldItem.php is as follows: /** * Plugin Implementation of the 'icon_field' field type. * @FieldType( * id = "icon_field", * label = @Translation("Icon Field"), * module = "icon_field", * description = @Translation("Store a bundle and icon in the database t

GSoC'19 Coding Period | Week #7 | Icon API

The Google Summer of Code has reached the end of the week #7 of the coding period with my project " Complete porting of Icons API to Drupal 8 " being progressing smoothly with interesting challenges along the way. In my previous blog post , I had mentioned that I'll be implementing the Icon Filter sub-module which will find and replace the pattern with the icon. The module replaces the following pattern with the icon: [icon:%bundle:%icon] where %bundle = Icon bundle machine name and             %icon = Icon name in the icon bundle During this week, I've searched on Google for implementing the Filter API in Drupal 8 and then I found Creating a Custom Filter in Drupal 8 article on lullabot.com which helped me in understanding and implementing the Icon Filter sub-module. In the Drupal 7 version of Icon Filter module was developed using the hooks but in the Drupal 8 it was replaced with the Plugin system which resides in the src/Plugin/Filter directory