How to create block with Form in Drupal 8

Blocks are the most important site building elements of Drupal 8. Many times there is a requirement of form in a block. It can be placed in following way. 

<?php
//Namespace for Example DrupalABC module.Replace it with your custom module.
namespace Drupal\example_drupalabc\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'HomePageSearchBlock' block.
*
* @Block( * id = "homepage_search_block",
* admin_label = @Translation("Home Page search block"),
* )
*/
class HomePageSearchBlock extends BlockBase {
public function build() {
$build = [];
$build['#cache']['contexts'][] = 'url';
$build['#cache']['max-age'] = 864000; $build['homepage_search_filter_block']['form'] = \Drupal::formBuilder() ->getForm('\Drupal\example_drupalabc\Form\HomePageSearchForm');
return $build;
}
}